Jump to content

Banner.jpg.b83b14cd4142fe10848741bb2a14c66b.jpg

Arduino serial bus help needed


Stu

Recommended Posts

I’m very much a beginner at this, and have been working my way through the projects in my Arduino Mega kit, received as a present last Christmas ie 2019 and just being used in the last week or so.

I managed to get the 16x2 display working, with the DS1307 RTC module giving date and time, a thermistor giving temp, and a DHT11 temp and humidity sensor giving humidity. I even managed to get a few point calculation on there too, with the display alternating between the date/time and Temp/humidity/dew point readings. All good so far.

I then ordered a 20x4 display, not necessarily realising that it had a serial board on the back giving a serial connection. Now, having received it, I love the simplicity and got it working very quickly but.... I now can’t get the DHT11 module working at the same time. From what I’ve read, I should be able to connect the serial bus connections together, with a pull up resistor and it should work, but all I get is zeros being output by the DHT11.

So, all you Arduino gurus out there!

Is it possible to run the DHT11 and the serial LCD panel on the same serial bus? If so, what am I doing wrong? Do I need to use the display directly rather than via the serial interface?

Video attached of my successful first go with the 2 line display.

Many thanks

Stu

Link to comment
Share on other sites

Hi,

I'm no Arduino expert, but have some experience with the  sensors and displays having designed and built this (with some help on the code)

S2460004.JPG

 

The DHT11 isn't really a serial device, it's a one wire device, in other words data travels down one wire, sometimes in both directions, often just in one, so data is sent from the device to the processor.  Other temperature sensors such as the DS18B20 use the same process, but the protocols (the way the data is sent) is different between the two one wire devices.

If you are using the same pin in the code for the DHT11 and serial connection then it will conflict.  Move the DHT data wire to a different pin, change the code to reflect that change and you should be OK

Edited by malc-c
  • Thanks 1
Link to comment
Share on other sites

1 hour ago, malc-c said:

Hi,

I'm no Arduino expert, but have some experience with the  sensors and displays having designed and built this (with some help on the code)

S2460004.JPG

 

The DHT11 isn't really a serial device, it's a one wire device, in other words data travels down one wire, sometimes in both directions, often just in one, so data is sent from the device to the processor.  Other temperature sensors such as the DS18B20 use the same process, but the protocols (the way the data is sent) is different between the two one wire devices.

The LCD serial board will use either TTL serial protocol, or I2C serial protocol, and there might be a jumper to select which one, or you might have to connect certain pins on a header depending on the board and protocol you want to use.  The Mega (and any other Arduino board for that matter that has more than one TTL serial port)  will always use the standard USB (serial 0) connection when the code uses the Serial statement Serial.begin (9600): If you want to connect the LCD using the TTL serial pins to the MEGA then you will need to use one of the other serial Ports.  Serial 1 uses pin 18 for TX and 19 for RX, and the code Serial1.begin(9600); to designate you want to use this port.

If you are using the same pin in the code for the DHT11 and serial connection then it will conflict.  Move the DHT data wire to a different pin, change the code to reflect that change and you should be OK

Thanks very much Malc. I seem to have confused myself! Of course it is the RTC which is a serial device, not the DHT11, DOH. I’m also using an Uno this instance, not a Mega.

I’ve got the display and RTC connected to the SCL and SCA lines, linking them on the breadboard and using pull up resistors.

The time and date function works as does the screen, so I assume that setup is correct. The DHT11 is connected to pin 2 and gives zero output on the display and the serial monitor. If I run a sketch just for the DHT11 outputting to serial then in functions correctly (without changing any physical connections)

So, I’m a bit flummoxed! Key code is the same between the standalone DHT11 and when it is in the larger sketch.

Link to comment
Share on other sites

19 hours ago, Stu said:

I now can’t get the DHT11 module working at the same time.

Welcome to the slippery slope! 🤖

There are at least four different sorts of "serial bus" available on an Arduino. All that serial bus means is that data is sent one bit at a time - serially. There are many ways to do that. The two that you have discovered are what is known as I2C which uses 2 Arduino pins and can support many devices, each with its own unique address, on the same 2 wires. The other one you have seen for the DTH11 is a proprietary (and somewhat unreliable) technique used to send a command to a device and then receive a reply back - all on the same single wire, just not at the same time.

Apart from these, your future holds an "rs232" talking to (typically) your PC - although the perfectionists will remark that rs232 is much more than just how the bits of a byte are sent down a wire.  And SPI that can send/receive bulk data very quickly, for example to draw images or text on an LCD screen.  All of these the Arduino can support on different pins at the same time ... if you're lucky.

 

Edited by pete_l
  • Thanks 1
Link to comment
Share on other sites

Ahhhh that would explain the confusion, do you mean Arduino Uno - No idea what a Arduino Uninin is. 

Yes the I2C serial is a BUS type protocol.  Two wires CLOCK (SCL) and DATA  (SDA) run out from the micro and are pulled high buy two resistors between 4.7K and 10K.  Each device with an I2C port is connected to the BUS via the two SCL and SDA lines.  As you have the real time clock module running and its displaying the time on the LCD, both connected to the I2C bus then that part is working.  If you have the DHT11 connected to PIN2  (digital) and have a standalone bit of code that successfully sends the results to the serial monitor, but then fails when you run the combined code, then it would suggest the issue is with the way the combined code is executed, or writen.

Can you copy and paste the complete code into Notebook and attach that to a reply and I'll take a look.... I'm not an expert, but something may be obvious as sometimes what looks logical doesn't always follow when it comes to Arduinos C++

Link to comment
Share on other sites

3 minutes ago, pete_l said:

Welcome to the slippery slope! 🤖

There are at least four different sorts of "serial bus" available on an Arduino. All that serial bus means is that data is sent one bit at a time - serially. There are many ways to do that. The two that you have discovered are what is known as "rs232" talking to your LCD - although the perfectionists will remark that rs232 is much more than just how the bits of a byte are sent down a wire. The other one you have seen for the DTH11 is a proprietary (and somewhat unreliable) technique used to send a command to a device and then receive a reply back - all on the same single wire, just not at the same time.

 

I think he's using the I2C bus for both devices rather than "rs232"... And in reality whilst the serial uses the RS232 protocol, it's not a true RS232 as it used TTL levels for the data bits where true RS232 swings  +/- through 12v (although the range is +/- 3 to 25v depending on the distance).

Link to comment
Share on other sites

10 minutes ago, malc-c said:

Ahhhh that would explain the confusion, do you mean Arduino Uno - No idea what a Arduino Uninin is. 

Yes the I2C serial is a BUS type protocol.  Two wires CLOCK (SCL) and DATA  (SDA) run out from the micro and are pulled high buy two resistors between 4.7K and 10K.  Each device with an I2C port is connected to the BUS via the two SCL and SDA lines.  As you have the real time clock module running and its displaying the time on the LCD, both connected to the I2C bus then that part is working.  If you have the DHT11 connected to PIN2  (digital) and have a standalone bit of code that successfully sends the results to the serial monitor, but then fails when you run the combined code, then it would suggest the issue is with the way the combined code is executed, or writen.

Can you copy and paste the complete code into Notebook and attach that to a reply and I'll take a look.... I'm not an expert, but something may be obvious as sometimes what looks logical doesn't always follow when it comes to Arduinos C++

Tee hee, I did indeed mean Uno, not that funny word that spell checker came up with!!

Sounds like the most likely solution, I will see if I can post the sketch up in a while, thanks for your help 👍

Link to comment
Share on other sites

1 hour ago, malc-c said:

Ahhhh that would explain the confusion, do you mean Arduino Uno - No idea what a Arduino Uninin is. 

Yes the I2C serial is a BUS type protocol.  Two wires CLOCK (SCL) and DATA  (SDA) run out from the micro and are pulled high buy two resistors between 4.7K and 10K.  Each device with an I2C port is connected to the BUS via the two SCL and SDA lines.  As you have the real time clock module running and its displaying the time on the LCD, both connected to the I2C bus then that part is working.  If you have the DHT11 connected to PIN2  (digital) and have a standalone bit of code that successfully sends the results to the serial monitor, but then fails when you run the combined code, then it would suggest the issue is with the way the combined code is executed, or writen.

Can you copy and paste the complete code into Notebook and attach that to a reply and I'll take a look.... I'm not an expert, but something may be obvious as sometimes what looks logical doesn't always follow when it comes to Arduinos C++

Here you go. I'm sure it's not pretty but it did work with the 16 x 2 panel

I'm using the thermistor for temperature (in part because it was returning figures to two decimal places vs integers from the DHT11, so am just using the DHT11 for humidity. I'm baffled because I've just repasted in the original code and no change. Any thoughts appreciated.

 

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

LiquidCrystal_I2C lcd(0x27,20,4);

DS3231 clock;
RTCDateTime dt;

float dpf=0; //Floating variable for Dew Point
int tempPin = 0;

#include <dht_nonblocking.h>
#define DHT_SENSOR_TYPE DHT_TYPE_11

static const int DHT_SENSOR_PIN = 2;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );

void setup()
{
  Serial.begin( 9600);
  lcd.init();
  lcd.backlight();
  
  // Initialize DS3231
  clock.begin();

  // Send sketch compiling time to Arduino
  clock.setDateTime(__DATE__, __TIME__);    
  /*
  Tips:This command will be executed every time when Arduino restarts. 
       Comment this line out to store the memory of DS3231 module
  */

}

/*
 * Poll for a measurement, keeping the state machine alive.  Returns
 * true if a measurement is available.
 */

 static bool measure_environment( float *temperature, float *humidity )
{
  static unsigned long measurement_timestamp = millis( );

  /* Measure once every four seconds. */
  if( millis( ) - measurement_timestamp > 3000ul )
  {
    if( dht_sensor.measure( temperature, humidity ) == true )
    {
      measurement_timestamp = millis( );
      return( true );
    }
  }

  return( false );
}

/*
 * Main program loop.
 */

void loop()
{
  float temperature;
  float humidity;
  
    int tempReading = analogRead(tempPin);
  // This is OK
  double tempK = log(10000.0 * ((1024.0 / tempReading - 1)));
  tempK = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * tempK * tempK )) * tempK );       //  Temp Kelvin
  float tempC = tempK - 273.15;            // Convert Kelvin to Celcius
  float tempF = (tempC * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
  /*  replaced
    float tempVolts = tempReading * 5.0 / 1024.0;
    float tempC = (tempVolts - 0.5) * 10.0;
    float tempF = tempC * 9.0 / 5.0 + 32.0;
  */
  // Display Temperature in C

  dt = clock.getDateTime();   // For leading zero look to DS3231_dateformat example
//timer=millis()/4000;
  
    lcd.setCursor(0, 0);
    lcd.print(dt.day);lcd.print("-");
    lcd.print(dt.month);lcd.print("-");
    lcd.print(dt.year);
    lcd.print("           ");
    lcd.setCursor(0,1);
    lcd.print(dt.hour);lcd.print("-");
    lcd.print(dt.minute);lcd.print("-");
    lcd.print(dt.second);lcd.print(" ");
    lcd.print("           ");
  
/* Measure temperature and humidity.  If the functions returns
     true, then a measurement is available. */
  
    lcd.setCursor(0, 2);
    lcd.print("Temp       C  ");
    lcd.setCursor(6, 2);
  // Display Temperature in C
    lcd.print(tempC);
  //lcd.print(temperature);

//   if( measure_environment( &temperature, &humidity ) == true )
  {
    lcd.setCursor(0, 3);
    lcd.print("Hum        %  ");
    lcd.setCursor(6, 3);
//  Display Temperature in C
//  lcd.print(tempC);
//  Display Humidity in %
    lcd.print(humidity);
    }
    
//  dpf=tempC-((100-humidity)/5); //calculate dew point

//  lcd.setCursor(0,2);
//  lcd.print("Dew P");
//  lcd.setCursor(0,3);
//  lcd.print("C");
//  lcd.setCursor(10,1);
//  lcd.print(dpf);

    Serial.print( "T = " );
    Serial.print( temperature, 1 );
    Serial.print( " deg. C, H = " );
    Serial.print( humidity, 1 );
    Serial.println( "%" );

  delay(1000);
 

}

Link to comment
Share on other sites

Strangely, I've just re-built the code but putting the humidity reading before the other stuff and it works.

No idea why!

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

#include <dht_nonblocking.h>
#define DHT_SENSOR_TYPE DHT_TYPE_11
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,20,4);

DS3231 clock;
RTCDateTime dt;

float dpf=0; //Floating variable for Dew Point
int tempPin = 0;

#include <dht_nonblocking.h>
#define DHT_SENSOR_TYPE DHT_TYPE_11

static const int DHT_SENSOR_PIN = 2;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );

/*
 * Initialize the serial port.
 */
void setup( )
{
  Serial.begin( 9600);
  lcd.init();
  lcd.backlight();

    // Initialize DS3231
  clock.begin();

  // Send sketch compiling time to Arduino
  clock.setDateTime(__DATE__, __TIME__);
  /*
  Tips:This command will be executed every time when Arduino restarts. 
       Comment this line out to store the memory of DS3231 module
  */    
}

/*
 * Poll for a measurement, keeping the state machine alive.  Returns
 * true if a measurement is available.
 */
static bool measure_environment( float *temperature, float *humidity )
{
  static unsigned long measurement_timestamp = millis( );

  /* Measure once every four seconds. */
  if( millis( ) - measurement_timestamp > 3000ul )
  {
    if( dht_sensor.measure( temperature, humidity ) == true )
    {
      measurement_timestamp = millis( );
      return( true );
    }
  }

  return( false );
}

/*
 * Main program loop.
 */
void loop( )
{
  float temperature;
  float humidity;

  int tempReading = analogRead(tempPin);
  // This is OK
  double tempK = log(10000.0 * ((1024.0 / tempReading - 1)));
  tempK = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * tempK * tempK )) * tempK );       //  Temp Kelvin
  float tempC = tempK - 273.15;            // Convert Kelvin to Celcius
  float tempF = (tempC * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
  /*  replaced
    float tempVolts = tempReading * 5.0 / 1024.0;
    float tempC = (tempVolts - 0.5) * 10.0;
    float tempF = tempC * 9.0 / 5.0 + 32.0;
  */
  // Display Temperature in C

  /* Measure temperature and humidity.  If the functions returns
     true, then a measurement is available. */
  if( measure_environment( &temperature, &humidity ) == true )
  {
    Serial.print( "T = " );
    Serial.print( temperature, 1 );
    Serial.print( " deg. C, H = " );
    Serial.print( humidity, 1 );
    Serial.println( "%" );

    lcd.setCursor(0, 3);
    lcd.print("H:       %  ");
    lcd.setCursor(3, 3);
    lcd.print(humidity);

    lcd.setCursor(0, 2);
    lcd.print("T:       C  ");
    lcd.setCursor(3, 2);
  // Display Temperature in C
    lcd.print(tempC);
  //lcd.print(temperature);

  dt = clock.getDateTime();   // For leading zero look to DS3231_dateformat example
//timer=millis()/4000;
  
    lcd.setCursor(0, 0);
    lcd.print(dt.day);lcd.print("-");
    lcd.print(dt.month);lcd.print("-");
    lcd.print(dt.year);
    lcd.print("           ");
    lcd.setCursor(0,1);
    lcd.print(dt.hour);lcd.print("-");
    lcd.print(dt.minute);lcd.print("-");
    lcd.print(dt.second);lcd.print(" ");
    lcd.print("           ");

  dpf=tempC-((100-humidity)/5); //calculate dew point

    lcd.setCursor(12,2);
    lcd.print("Dew Pt");
    lcd.setCursor(18,3);
    lcd.print("C");
    lcd.setCursor(12,3);
    lcd.print(dpf);
    
  }
}

Link to comment
Share on other sites

32 minutes ago, Stu said:

Strangely, I've just re-built the code but putting the humidity reading before the other stuff and it works.

No idea why!

 

Welcome to the world of Arduino....

I think that is more logical... at the start of the loop you read the sensors, then read the clock and then display them on the screen, then loop round again.

I also noted that the code has the DTH connected to pin 0, which is not a digital input, its part of the serial port... it might be better moving it two pins above to pin 2 and change the int tempPin = 0; to int tempPin = 2;

Link to comment
Share on other sites

Thank you @malc-c

Some strange things occurring. Same code and connections works ok on the Mega but the Humidity reading doesn’t work on the Uno.

Also, the mega is now only refreshing the display every 3 seconds, whereas when the Humidity is not reading it counts each second.

BTW, I am using a thermistor for temperature so The tempPin is the setting for the A0, the analogue pin. Does that seem right?

I’m using pull-up resistors on the I2C now as it seems to work fine without them.

Link to comment
Share on other sites

Stu, with all due respects you tell us one thing but do another... You state its a Uno not a Mega that you are using, then vice versa... your mention DHT11, the code has the library and uses the DHT commands... but then you state you are using a thermistor...  you ask about humidity which relates to the DHT, but you have stated you ain't using... So how the hell can we advise you when none of us actually have an idea of exactly what sensors you are using and on what platform....   Pin assignments change from platform to platform.... so telling you to use one pin is meaningless when you are no longer using  that platform.

I2C needs two pull up resistors og 4.7k min, one on the clock the other on the data lines.

I would also suggest googling and reading up on how the DHT sensor sends its data packets, and you will get some idea of the timing which may delay the loop.

Link to comment
Share on other sites

Thanks Malc. I think you’ve misinterpreted what I’ve said, and I was just poking my head above the parapet in terms of learning this stuff hoping for advice.

I’ll close the thread.

Stu

  • Sad 1
Link to comment
Share on other sites

  • Stu locked this topic
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. By using this site, you agree to our Terms of Use.