Ethernet.begin() preventin lcd.print() with 16x2 display - arduino

I am trying to implement a webserver using the W1500 shield (I think it's called that) that also links to a 16x2 LCD and some DHT22 sensors. In my setup function, I have Ethernet.begin(mac, ip); but I can only utitlize the lcd.print() function before calling Ethernet.begin(). After calling this, any lcd.print() does not have any effect (even in the loop, the print function does nothing).
What would cause this and how can I get around this?
Thank you!
(This is my first post, I'm bound to have done something wrong so please let me know!)
#include "DHT.h"
#include <SPI.h>
#include <Ethernet.h>
#include <Vector.h>
#include <LiquidCrystal.h>
#define DHTPIN1 6
#define DHTPIN2 7
#define DHTPIN3 8
#define DHTTYPE DHT22
DHT dht[] = {
{DHTPIN1, DHT22},
{DHTPIN2, DHT22},
{DHTPIN3, DHT22},
};
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
byte ip[] = { 10, 0, 0, 99 };
EthernetServer server(80);
float h[3];
float t[3];
float tf[3];
float avgTempC = 0.00;
float avgTempF = 0.00;
float avgHumid = 0.00;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup()
{
lcd.begin(16, 2);
// THIS CORRECTLY PRINTS TO LCD
lcd.print("TEMP: HUMID:");
for (auto& sensor : dht) {
sensor.begin();
}
Ethernet.begin(mac, ip);
server.begin( );
// HERE IS WHERE IT DOES NOT WORK
String avgTString = String(avgTempF);
String avgHString = String(avgHumid);
lcd.setCursor(0, 1);
lcd.print(avgTString + "F");
lcd.setCursor(8, 1);
lcd.print(avgHString + "%");
Serial.print("server.begin() called\n");
}
void loop() {...}

You did not mention which ethernet board and which microcontroller board you actually use.
Please check which pins are used by your ethernet shield. Please have a look at the technical description of your ethernet shield.
If you have - for example - an Arduino Ethernet Shield V1, you find the details here:
https://www.arduino.cc/en/Main/ArduinoEthernetShieldV1
Arduino communicates with both the W5100 and SD card using the SPI bus (through the ICSP header). This is on digital pins 10, 11, 12, and 13 on the Uno and pins 50, 51, and 52 on the Mega. On both boards, pin 10 is used to select the W5100 and pin 4 for the SD card. These pins cannot be used for general I/O. On the Mega, the hardware SS pin, 53, is not used to select either the W5100 or the SD card, but it must be kept as an output or the SPI interface won't work.
You cannot use these pins to communicate with the LCD - at least if the LCD does not also use SPI (e.g. if it is connected via 74HC595 shift registers) and even then, you'd have to deselect the other SPI devices and then select the LCD device.
For details, see:
https://www.arduino.cc/en/reference/SPI

Related

How to connect MAX30100 pulse sensor to a different i2c pins of ESP32 and read data?

I'm using a ESP32 30 pin board, MAX30100 pulse sensor for my project.
I can interface this sensor to ESP32's different i2c pins i.e. not default pins(21,22).
But I don't know how to read data from the MAX30100 if it connected to different pins - (Let's say 32, 33)
This is the program I used for default i2c pins to read data from MAX30100
#include <Wire.h>
#include "MAX30100_PulseOximeter.h"
#define BLYNK_PRINT Serial
#include <Blynk.h>
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#define REPORTING_PERIOD_MS 1000
char auth[] = "*******************"; // You should get Auth Token in the Blynk App.
// Connections : SCL PIN - D1 , SDA PIN - D2 , INT PIN - D0
PulseOximeter pox;
float BPM, SpO2;
uint32_t tsLastReport = 0;
void onBeatDetected()
{
Serial.println("Beat Detected!");
}
void setup()
{
Serial.begin(115200);
pinMode(19, OUTPUT);
Blynk.begin(auth,"************", "**********");
Serial.print("Initializing Pulse Oximeter..");
if (!pox.begin()) {
Serial.println("FAILED");
for(;;);
}
else
{
Serial.println("SUCCESS");
pox.setOnBeatDetectedCallback(onBeatDetected);
}
// The default current for the IR LED is 50mA and it could be changed by uncommenting the following line.
pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
}
void loop()
{
pox.update();
Blynk.run();
BPM = pox.getHeartRate();
SpO2 = pox.getSpO2();
if (millis() - tsLastReport > REPORTING_PERIOD_MS)
{
Serial.print("Heart rate:");
Serial.print(BPM);
Serial.print(" bpm / SpO2:");
Serial.print(SpO2);
Serial.println(" %");
Blynk.virtualWrite(V3, BPM);
Blynk.virtualWrite(V4, SpO2);
tsLastReport = millis();
}
}
How do I interface MAX30100 to other pins? What should be the instructions?
PulseOximeter pox;
What does this instruction mean?
You should change direction in library:
Link to library file in github
Line 29: change that for
Wire.begin(SDA_PIN, SCL_PIN);
Where SDA_PIN and SCL_PIN are defines of your own routing.
NOTE: if you change the library you will need always to use that pins for all of your developments so maybe is better if you import that library locally or even better if you change library so if you don't pass any pin at argument it default normally I2C pins but, if defined use the defined ones.

Arduino can't output in both led screen and serial monitor at the same time

I am trying to have Arduino output on both LED screen using the LiquidCrystal library, and on Serial Monitor (later to a txt file or something like that).
In my code, I commented out Serial.begin(9600) and then the screen outputs correctly, but as soon as I include it, the serial monitor outputs fine but the screen flips out and outputs gibberish.
I'm fairly new and I know there's something basic I don't know like 9600 should be augmented because so much power is needed maybe?
#include <LiquidCrystal.h>
#include <DHT.h>
#include <math.h>
/*
* Cannot do both screens and log in the console.
* Currently Serial 9600 is commented out, to allow to print on the screen
* Need Fixing
*/
#include "DHT.h"
#define DHTPIN 8 // what digital pin we're connected to
#define DHTTYPE DHT11 // DHT 11
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal lcd(1, 2, 4, 5, 6, 7);
void setup() {
//Serial.begin(9600);
Serial.println("Temperature Recorder");
dht.begin();
// Now LiquidCrystal led monitor stuff
lcd.begin(16,2);
lcd.setCursor(2,0);
lcd.print("** Wanet **");
delay(1500);
lcd.setCursor(1,1);
lcd.print("Motherfuckers.");
delay(3000);
lcd.clear();
}
void loop() {
// Wait a few seconds between measurements.
delay(1000);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print(" | Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.println(" *F");
Serial.println("------------------------------------");
// led screen printing
lcd.setCursor(0,0);
lcd.print("Temp: Humidity:");
lcd.setCursor(0,1);
lcd.print(t);
lcd.print(" ");
lcd.print(round(f));
lcd.print("%");
delay(5000);
lcd.clear();
lcd.setCursor(2,0);
lcd.print("The world");
lcd.setCursor(4,1);
lcd.print("OURS");
delay(6000);
}
Cheers
On the docs of Arduino's Serial
Serial
It communicates on digital pins 0 (RX) and 1 (TX) as well as with the computer via USB. Thus, if you use these functions, you cannot also use pins 0 and 1 for digital input or output.
You have two options or you don't use those 2 pins
like this
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
or if you must have those pin you might consider to use a library like softwareSerial which emulates the serial communication a pair of pin of your choice. but the serial monitor via USB won't work anyway.
Ah right so if you are using LiquidCrystal lcd(6, 5, 4, 3, 2, 1);
You cannot use serial.println(""); too because they conflict on D1 / TX->
so would it then be possible to use both lcd.print(""); and serial.println(""); if we shift to pins
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
or am I misunderstanding?
Use other Arduino pins for the lcd display. D1 is shared with serial communication.

code don't work on my d18b20 temperature sensor

The simple code for arduino is down the page and everything works (it shows me temperature as it is), but in this example I include ethernet and begin internet and it doesn't show me temperature anymore....in serial monitor it just shows me temperature -127.00, so it mean it doesn't work. I am very new at programming, so I am sorta noob and maybe it's some banal question and answer, but I don't get it, why I cant start ethernet in setup function. I only need that for my other project which will include mqtt, so I will need ethernet. Please help and thanks for answers.
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Ethernet.h>
// Data wire is plugged into pin 12 on the Arduino
#define ONE_WIRE_BUS 12
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 80);
void setup(void)
{
Serial.begin(9600); //Begin serial communication
Ethernet.begin(mac, ip);
Serial.println("Arduino Digital Temperature // Serial Monitor Version"); //Print a message
sensors.begin();
}
void loop(void)
{
// Send the command to get temperatures
sensors.requestTemperatures();
Serial.print("Temperature is: ");
Serial.println(sensors.getTempCByIndex(0)); // Why "byIndex"? You can have more than one IC on the same bus. 0 refers to the first IC on the wire
//Update value every 1 sec.
delay(1000);
}

Multiple Slave SPI on ESP8266 - PN532 and ILI9341

Im trying to interface ESP12-E module with ILI9341 display 320*240 And PN532 RFID reader on the same SPI bus. I have assigned SS Pins on different GPIO.
Im unable to Communicate with both. Display works perfectly in any conditions. But once i communicate with ILI9341 , the PN532 stops working and it will not respond until i restart the device even if i reinitialize it.
Any Help would be highly Appreciated
My Code :
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>
#include <UTFT.h>
UTFT lcd(ILI9341_S5P,15,D1,D3);
// If using the breakout with SPI, define the pins for SPI communication.
#define PN532_SS (D4)
#define PN532_SCK (D5)
#define PN532_MOSI (D7)
#define PN532_MISO (D6)
// If using the breakout or shield with I2C, define just the pins connected
// to the IRQ and reset lines. Use the values below (2, 3) for the shield!
#define PN532_IRQ (2)
#define PN532_RESET (3) // Not connected by default on the NFC Shield
// Uncomment just _one_ line below depending on how your breakout or shield
// is connected to the Arduino:
// Use this line for a breakout with a SPI connection:
Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
// Use this line for a breakout with a hardware SPI connection. Note that
// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's
// hardware SPI SCK, MOSI, and MISO pins. On an Arduino Uno these are
// SCK = 13, MOSI = 11, MISO = 12. The SS line can be any digital IO pin.
//Adafruit_PN532 nfc(PN532_SS);
// Or use this line for a breakout or shield with an I2C connection:
//Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
extern uint8_t BigFont[];
void setup(void) {
Serial.begin(115200);
Serial.println("Hello!");
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find PN53x board");
while (1); // halt
}
// Got ok data, print it out!
Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
lcd.InitLCD();
lcd.setColor ( 0, 0, 0 );
lcd.fillRect(1,1,319,239);
lcd.setColor ( 255, 255, 255 );
lcd.fillRect(100,100,220,140);
lcd.setFont ( BigFont );
lcd.print(String("Scanning"),0,0);
// Set the max number of retry attempts to read from a card
// This prevents us from waiting forever for a card, which is
// the default behaviour of the PN532.
nfc.setPassiveActivationRetries(0xFF);
// configure board to read RFID tags
nfc.SAMConfig();
Serial.println("Waiting for an ISO14443A card");
}
void loop(void) {
boolean success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
// Wait for an ISO14443A type cards (Mifare, etc.). When one is found
// 'uid' will be populated with the UID, and uidLength will indicate
// if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength,25);
if (success) {
Serial.println("Found a card!");
Serial.print("UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
Serial.print("UID Value: ");
for (uint8_t i=0; i < uidLength; i++)
{
Serial.print(" 0x");Serial.print(uid[i], HEX);
}
Serial.println("");
// Wait 1 second before continuing
delay(1000);
}
else
{
// PN532 probably timed out waiting for a card
//Serial.println("Timed out waiting for a card");
}
}
I've testing it a lot and figured out - The SPI bit-order is different, so you can not drive these two devices on the same SPI.
But you can use SW-SPI (bitbanging) for the NFC module, because it is not necessary to drive it fast. The TFT instead must be fast to have good update rates.
One Issue is still open on my setup:
the display did not show content, if you have an open serial terminal. But when it is closed, both TFT and NFC are working fine together.
Here is my code:
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>
#include <UTFT.h>
// NFC module
#define PN532_SCK D1
#define PN532_MOSI D2
#define PN532_MISO D3
#define PN532_SS D0
Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
// TFT display
// HSPI defines
#define TFT_SCK D5
#define TFT_MOSI D7
//#define TFT_MISO D6 (not connected)
#define TFT_CS D8
#define TFT_DC D4
UTFT myGLCD(ILI9341_S5P, TFT_CS, -1, TFT_DC);
// Declare which fonts we will be using
extern uint8_t SmallFont[];
extern uint8_t BigFont[];
// forward declaration of helper function to get UID as HEX-String
void byteToHexString(String &dataString, byte *uidBuffer, byte bufferSize, String strSeperator);
void setup() {
Serial.begin(9600);
Serial.println("Initial nfc module");
nfc.begin();
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find PN53x board");
while (1); // halt
}
// Got ok data, print it out!
Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
// Set the max number of retry attempts to read from a card
// This prevents us from waiting forever for a card, which is
// the default behaviour of the PN532.
nfc.setPassiveActivationRetries(0xFF);
// configure board to read RFID tags
nfc.SAMConfig();
Serial.println("Waiting for an ISO14443A card");
Serial.println("Initial tft display");
myGLCD.InitLCD();
myGLCD.setColor(0, 0, 0);
myGLCD.fillRect(1,1,319,239);
myGLCD.setColor(255, 255, 255);
myGLCD.setFont(BigFont);
myGLCD.print(String("Scanning"),0,0);
}
void loop(void) {
boolean success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
// Wait for an ISO14443A type cards (Mifare, etc.). When one is found
// 'uid' will be populated with the UID, and uidLength will indicate
// if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
//success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength,25);
if (success) {
Serial.println("Found a card!");
Serial.print("UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
Serial.print("UID Value: ");
String strUID;
// store UID as HEX-String to strUID and print it to display
byteToHexString(strUID, uid, uidLength, "-");
Serial.println("");
Serial.println(strUID);
myGLCD.print(strUID, CENTER, 50);
myGLCD.setColor ( 255, 0, 0 );
myGLCD.setFont ( BigFont );
myGLCD.print(String("Scanning"),0,0);
// Wait 1 second before continuing
delay(1000);
}
else
{
Serial.println("Timed out or waiting for a card");
}
}
// helper function to get UID as HEX-String
void byteToHexString(String &dataString, byte *uidBuffer, byte bufferSize, String strSeperator=":") {
dataString = "";
for (byte i = 0; i < bufferSize; i++) {
if (i>0) {
dataString += strSeperator;
if (uidBuffer[i] < 0x10)
dataString += String("0");
}
dataString += String(uidBuffer[i], HEX);
}
dataString.toUpperCase();
}
Thanks for the help.
Im not a big fan of bitbanging SPI.
I have tried several ways and tried re-initializing, etc.
Finally, i got it solved.
I dont know why, but when i re-initialize spi hardware after reading card, Both of the module works perfectly.
The code i added just after reading card:
lcd._hw_special_init();
like:
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, &uid[0], &uidLength,0);
if (success) {
lcd._hw_special_init();
//Do the rest
}

LCD 16x2 failed to display Hello world using NodeMCU1.0

I am doing a small project to display text on LCD using NodeMCU1.0. The problem is that the text is not displayed when I upload the sketch "Blank screen only"?
Any help?
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
// Arduino UNO => PIN 4 = SCL, PIN 5 = SDA
// NodeMCU Dev Kit => D1 = SCL, D2 = SDA
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup()
{
// initialize the LCD
lcd.begin();
// Turn on the blacklight and print a message.
lcd.backlight();
lcd.print("Hello World");
}
void loop(){
}
LiquidCrystal_I2C lcd(0x27, 20, 4);
change to...
LiquidCrystal_I2C lcd(0x27, 16, 2); //for 16x2 lcd.
then compile and upload ur code, it may be helpful for u.

Resources