2

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

3 Answers 3

3

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.

2
  • Thanks. So mean digital 0 and 1 need to have nothing plugged in them, whenever I want to do serial communication?
    – J.C
    Jul 14, 2018 at 7:35
  • 2
    Yes whenever you want to do serial communication using the arduino usb! Because if you want to do serial communication between two arduino you can use softwareSerial and use other pins. Jul 14, 2018 at 8:03
1

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?

1
  • Thanks, a lot, man, that totally worked.
    – Pranta
    Jan 3, 2023 at 11:52
0

Use other Arduino pins for the lcd display. D1 is shared with serial communication.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.