Sketch Arduino with potentiometer + servomotor + DC motor + LCD + DHT11 + DS3231

I made a complex sketch for Arduino Uno board to which they are connected:

  • potentiometer,
  • servomotor,
  • DC motor,
  • L293D,
  • LCD display (LCD module 1602),
  • DHT11 sensor (temperature and humidity),
  • infrared sensor (IR receiver),
  • DS3231 RTC module ,
  • power supply module,
  • 5 buttons
  • LED.

I’ll explain how the sketch, that I attach at the bottom, works .
With the on / off button on the remote control, the LED and the DC motor light up.
But if the temperature and humidity by DHT11 are both lower than those set, the DC motor remains off.
(The temperature and humidity values are setting up respectively 30 ° and 40% and can be regulated by 4 buttons: one increases the temperature, one decreases the temperature, one increases the humidity and another decreases the humidity)

Then there is a button used to turn the servomotor on and off, if it is on, it moves from 0 to 180 degrees and from 180 to 0 degrees continuously.

The potentiometer adjusts the DC motor speed when it is on.

The temperature and humidity of the DHT11 sensor are shown in the LCD display on the first line.
Then if I change the set temperature with the buttons, it is shown on the second row of the screen.

The servomotor can also be switched on by the remote control and the set temperature and humidity can also be increased or decreased by the remote control buttons.
Everything working up to this point.

In addition to this I wanted to add the date and time given by the DS3231 RTC module and display them in the second line of the LCD display, but if I add the part of this code nothing works anymore and the LCD display shows nothing.
I do not understand if there is an error in the programming code, after the compilation I do not report any errors and the upload is completed.

I also write you what you see in the Arduino serial monitor:

  • WHEN I REMOVE THE PART OF THE DS3231 RTC CODE FROM THE SKETCH
    when I press a button on the remote control, it shows me the name of the button that I have pressed, then it displays when the servomotor is switched on and off and also when I increase or decrease the set temperature for switching on the DC motor using the buttons

  • WHEN I ADD THE PART OF THE DS3231 RTC CODE
    I would like to display the date and time in the serial monitor, but this does not happen
    The serial monitor shows only DS3231 initialization.
    The button pressed on the remote control are not displayed and what was displayed before is no longer seen in the serial monitor, as if the code part of the DS3231 module prevented everything else from working.

So I ask you there is an error in the programming code?
Now I am attaching the sketch.

#include <Wire.h>
#include <DS3231.h>

DS3231 clock;
RTCDateTime dt;

#include <LiquidCrystal.h>
#include <dht11.h>

#include <Servo.h>
#include "IRremote.h"

#define ENABLE 5
#define DIRA 3
#define DIRB 4

dht11 DHT11;
#define DHTPIN 13

int temperature=30;
int humidity=40;

#define PULSANTE A1
const int PULSANTESU=A2;
const int PULSANTEGIU=A3;

const int PULSANTESUum=A4;
const int PULSANTEGIUum=A5;

int PinMotore = 5;
int PotPin = A0;
int lettura = 0;
int velocita = 0;

int receiver =0;
int led = 2;
int i=0;


int buttonStatesuum = 0;
int buttonStategiuum = 0;
int buttonStatesu = 0;
int buttonStategiu = 0;
int pos=0;
int stato=0;
int cod=0;
const int PinServo=6;
int tempimpostata=30;
int umiditaimpostata=40;

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
Servo myservo;

IRrecv irrecv(receiver); 
decode_results results;     

void setup() {

  Serial.begin(9600);
  Serial.println("IR Receiver Button Decode"); 
  irrecv.enableIRIn(); // Start the receiver
  
  pinMode(led,OUTPUT);
  
  pinMode(PinMotore, OUTPUT); 
  
  pinMode(PULSANTE, INPUT);
  pinMode(PULSANTESU, INPUT);
  pinMode(PULSANTEGIU, INPUT);
   pinMode(PULSANTESUum, INPUT);
  pinMode(PULSANTEGIUum, INPUT);
  
  myservo.attach(PinServo);  
  myservo.write(0);
  
  lcd.begin(16, 2);
  pinMode(ENABLE,OUTPUT);
  pinMode(DIRA,OUTPUT);
  pinMode(DIRB,OUTPUT);

  Serial.println("Inizializzazione DS3231");
  clock.begin();
  clock.setDateTime(__DATE__, __TIME__);

}

void loop() {

  //DATA E ORA
  
  dt = clock.getDateTime();
  
  lcd.setCursor(0,1);
 
  lcd.print(clock.dateFormat("d-m-Y H:i:s", dt));

  Serial.print("DATA E ORA:         ");
  Serial.println(clock.dateFormat("d-m-Y H:i:s", dt));
  
  delay(1000);

//SENSORE DI TEMPERATURA

  int chk = DHT11.read(DHTPIN);
  lcd.setCursor(0, 0);
  lcd.print("Temp ");
  lcd.print((float)DHT11.temperature, 0);
  lcd.print(" C ");
  lcd.setCursor(10, 0);
  lcd.print((float)DHT11.humidity, 0);
  lcd.print("%"); 
   
  delay(2000);

//PULSANTI

  buttonStatesu = digitalRead(PULSANTESU);

  if (buttonStatesu == HIGH) 
  {
   tempimpostata = tempimpostata+ 1;
   lcd.setCursor(0, 1);
   lcd.print("Temp ");
   lcd.print(tempimpostata);
   lcd.print(" C ");
   Serial.println("temp + 1");
  } 

  buttonStategiu = digitalRead(PULSANTEGIU);
  if (buttonStategiu == HIGH) 
  {
  tempimpostata = tempimpostata- 1;
  lcd.setCursor(0, 1);
  lcd.print("Temp ");
  lcd.print(tempimpostata);
  lcd.print(" C ");
  Serial.println("temp - 1");
  } 
  
  buttonStatesuum = digitalRead(PULSANTESUum);

  if (buttonStatesuum == HIGH) 
  {
    umiditaimpostata = umiditaimpostata+ 1;
    lcd.setCursor(10, 1);
    lcd.print(umiditaimpostata);
    lcd.print("%");
    Serial.println("umidita + 1");
  } 

  buttonStategiuum = digitalRead(PULSANTEGIUum);
  if (buttonStategiuum == HIGH) 
  {
  umiditaimpostata = umiditaimpostata - 1;
  lcd.setCursor(10, 1);
  lcd.print(umiditaimpostata);
  lcd.print("%");
  Serial.println("umidita - 1");
  } 
  
  if( (temperature >= tempimpostata || humidity >= umiditaimpostata) && (i==1) )
  {
  analogWrite(ENABLE,255); //enable on
  digitalWrite(DIRA,HIGH); //one way
  digitalWrite(led,HIGH);
  }
  else if( (temperature < tempimpostata && humidity < umiditaimpostata) || (i==0) )
  { 
  analogWrite(ENABLE,0); //enable on
  digitalWrite(DIRA,LOW); //one way
  digitalWrite(led,LOW);
  }

//TELECOMANDO
  if (irrecv.decode(&results))
  {
    if(results.value == 0xFFA25D)
    {
      Serial.println("POWER"); 
      digitalWrite(led,HIGH);
      i=1;
    }

    if(results.value == 0xFFE21D)
    {
      Serial.println("FUNC/STOP");  
      digitalWrite(led,LOW);
      i=0;
    }

    if(results.value == 0xFF906F)
    {
      Serial.println("UP");    
      tempimpostata = tempimpostata+ 1;
     lcd.setCursor(0, 1);
     lcd.print("Temp ");
     lcd.print(tempimpostata);
     lcd.print(" C ");
    }

    if(results.value == 0xFFE01F)
    {
      Serial.println("DOWN");    
      tempimpostata = tempimpostata - 1;
     lcd.setCursor(0, 1);
      lcd.print("Temp ");
     lcd.print(tempimpostata);
     lcd.print(" C ");
    }

    if(results.value == 0xFF22DD)
    {
      Serial.println("FAST BACK");
     umiditaimpostata = umiditaimpostata - 1;
    lcd.setCursor(10, 1);
     lcd.print(umiditaimpostata);
    lcd.print("%");
    }

    if(results.value == 0xFFC23D)
    {
      Serial.println("FAST FORWARD");
      umiditaimpostata = umiditaimpostata + 1;
     lcd.setCursor(10, 1);
     lcd.print(umiditaimpostata);
     lcd.print("%");
     }

    if(results.value == 0xFF30CF)
    {
      Serial.println("1 servo acceso"); 
      cod=1;
     }
     
     if(results.value == 0xFF6897)
      {
        Serial.println("0 servo spento"); 
        cod=0;
      }

    irrecv.resume(); 
    
  } 

  //SERVO
  stato = digitalRead(PULSANTE);
  if(stato==HIGH && cod==0)
  {
    cod=1;
     Serial.println("servo acceso");
  }
  else if(stato==HIGH && cod==1)
  {
    cod=0;
    myservo.write(0);
     Serial.println("servo spento");
  }

 if(cod==1)
  {
     for (pos = 0; pos <= 180; pos += 1) { 
    // in steps of 1 degree
    myservo.write(pos);             
    delay(15);                     
     }
  for (pos = 180; pos >= 0; pos -= 1) { 
    myservo.write(pos);              
    delay(15);                       
    }
 
  }

  //POTENZIOMETRO  
  lettura = analogRead(PotPin); //Leggo il valore del potenziometro
  velocita = map(lettura, 0, 1023, 0, 255); //mappo i valori da 1024 a 255
  azione(velocita); //richiamo la funzione passondogli la velocità
  
}

void azione(int velocita)
{
  analogWrite(PinMotore, velocita);
}

Your text is very hard to read because it is all one long paragraph. See how some white-space helps to separate the different pieces

I made a complex sketch for Arduino Uno board to which they are connected:

  • potentiometer,
  • servomotor,
  • DC motor,
  • L293D,
  • LCD display (LCD module 1602),
  • DHT11 sensor (temperature and humidity),
  • infrared sensor (IR receiver),
  • DS3231 RTC module ,
  • power supply module,
  • 5 buttons and a
  • LED.

I'll explain how the sketch, that I attach at the bottom, works .

With the on / off button on the remote control, the LED and the DC motor light up.

But if the temperature and humidity by DHT11 are both lower than those set, the DC motor remains off.
(The temperature and humidity values are setting up ​​respectively 30 ° and 40% and can be regulated by 4 buttons: one increases the temperature, one decreases the temperature, one increases the humidity and another decreases the humidity)

Then there is a button used to turn the servomotor on and off, if it is on, it moves from 0 to 180 degrees and from 180 to 0 degrees continuously.

The potentiometer adjusts the DC motor speed when it is on.

The temperature and humidity of the DHT11 sensor are shown in the LCD display on the first line.
Then if I change the set temperature with the buttons, it is shown on the second row of the screen.

The servomotor can also be switched on by the remote control and the set temperature and humidity can also be increased or decreased by the remote control buttons.

Everything working up to this point.

In addition to this I wanted to add the date and time given by the DS3231 RTC module and display them in the second line of the LCD display, but if I add the part of this code nothing works anymore and the LCD display shows nothing.

I do not understand if there is an error in the programming code, after the compilation I do not report any errors and the upload is completed.

I also write you what you see in the Arduino serial monitor:

  • WHEN I REMOVE THE PART OF THE DS3231 RTC CODE FROM THE SKETCH
    when I press a button on the remote control, it shows me the name of the button that I have pressed, then it displays when the servomotor is switched on and off and also when I increase or decrease the set temperature for switching on the DC motor using the buttons
  • WHEN I ADD THE PART OF THE DS3231 RTC CODE
    I would like to display the date and time in the serial monitor, but this does not happen
    The serial monitor shows only DS3231 initialization.

The button pressed on the remote control are not displayed and what was displayed before is no longer seen in the serial monitor, as if the code part of the DS3231 module prevented everything else from working.

So I ask you there is an error in the programming code?
Now I am attaching the sketch.

Your program appears to include the RTC code. Please list the line-numbers of the fewest lines that must be commented-out in order to get the program to work?

...R

These are the parts of code that must be checked out.

#include <Wire.h>
#include <DS3231.h>

DS3231 clock;
RTCDateTime dt;
void setup() {

  Serial.println("Inizializzazione DS3231");
  clock.begin();
  clock.setDateTime(__DATE__, __TIME__);

}
void loop() {

  //DATA E ORA
  
  dt = clock.getDateTime();
  
  lcd.setCursor(0,1);
 
  lcd.print(clock.dateFormat("d-m-Y H:i:s", dt));

  Serial.print("DATA E ORA:         ");
  Serial.println(clock.dateFormat("d-m-Y H:i:s", dt));
  
  delay(1000);

}

Let’s take this step by step.

In Reply #2 you have three groups of lines that you need to exclude.

What happens if you ONLY exclude the lines in groups 2 and 3?

…R

Thank you for your reply!!

If I only exclude group 2 and 3 the sketch works.

But the problem is that I would like to show the date and the time on the LCD display on the second line.

So why doesn't this part of the sketch work?

Now try the code with just group 3 excluded and see what happens.

The idea is to narrow down the cause of the problem.

...R

If I just exclude group 3 the sketch doesn’t work.

The same if I just exclude the group 2.

OK. So now let's narrow things further - I'm assuming that group 3 is commented out so we can focus just on group 2

  Serial.println("Inizializzazione DS3231");
  clock.begin();
  clock.setDateTime(__DATE__, __TIME__);

Start with all of those lines commented out - it should work.

Then un-comment the first line and see what happens. Then the second line etc.

...R

  clock.begin();
  clock.setDateTime(__DATE__, __TIME__);

These are the lines of group 2 that prevent the sketch from working.

berax:

  clock.begin();

clock.setDateTime(DATE, TIME);




These are the lines of group 2 that prevent the sketch from working.

Does it work with only the first line enabled?

At this stage my guess is that the DS3231 library is incompatible with some of the other libraries you are using. To identify which one you would need to go through the same elimination exercise with all of the other libraries.

And if you do find the library that does not co-habit with the DS3231 library then you would need to examine the source code of both libraries to find out exactly what is causing the conflict.

I do appreciate that none of this is "good news"

A slight shortcut may be to examine the code in the DS3231 library and see if you could access the RTC without using the library.

I am assuming, of course, that there is no problem with how everything is connected.

...R

Robin2:
Does it work with only the first line enabled?

YES

Robin2:
At this stage my guess is that the DS3231 library is incompatible with some of the other libraries you are using. To identify which one you would need to go through the same elimination exercise with all of the other libraries.

And if you do find the library that does not co-habit with the DS3231 library then you would need to examine the source code of both libraries to find out exactly what is causing the conflict.

I tried to compile the sketch with only the part of the DS3231 library's code but it doesn't work.
So I think there isn't an incompatibility between the libraries.

I also tried to disable each library and the concerning code one by one but the problem persists.

Robin2:
I am assuming, of course, that there is no problem with how everything is connected.

Probably not. Could it be the only explanation to the problem?

berax:
I also tried to disable each library and the concerning code one by one but the problem persists.

You probably need to disable them all and then re-enable them one by one - very very tedious!

Probably not. Could it be the only explanation to the problem?

I have no idea. I don't have your hardware.

...R

Robin2:
You probably need to disable them all and then re-enable them one by one - very very tedious!

Thank you very much!!

I found the problem there was an interference between the DS3231 and two buttons connected.

Thanks to your advice I've tried to disable all and re-able each part of code one by one and at the same time I've disconnected the corresponding hardware components.