Conflicts at Arduino Leonardo

Hey guys.
I've been doing a project that uses 2 sensors and 1 display:

  • DHT22 humidity sensor;
  • BMP180 atmosferic pressure sensor;
  • Nokia 5110 LCD display.
    I'm following a guide that was made for arduino Uno, which is in the image below:

I figured that I have to use the SCL / SDA pins next to the reset button for the I²C communication with the pressure sensor.
I'm using the LCD's SPI in D8-D12, just as in the image above.
I'm using the one-wire data from the humidity sensor in D3 (I tried it in other digital pins), as in the image.

When I try to use it all together, the pressure sensor works fine, but I can't receive any information from the humidity one. It fails to initialize and I receive 0 / nan.

What works:
pressure sensor + LCD
humidity sensor + LCD

What does not work:
pressure + humidity + LCD
LCD presents some fading out.

I'd like to know what is making the sensors conflict. I don't know how to "adapt" the pins from Uno to Leonardo. I am also in doubt about using the SPI as in the image or using the ICSP pins.

Can someone please help me?
Thanks,

You want us to answer without knowing what the code does?

Please put your code in its own window as seen in other posts. This can be done by placing     [code] and [/code]  around the code. This makes it easier for others to read.

Weedpharma

In additiion to what @weedpharma has said ...

Please describe exactly and completely the differences between the three options - the two that work and the third that does not. This may also involve posting 3 different codes.

...R

How are you supplying power to the Leonardo and the three devices?

@Hackscribble
I'm powering all of it through an USB port. Do you think that it may be caused by insufficient current?

@weedpharma
The code is as follow:

#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include <DHT.h>
#include <Adafruit_BMP085.h>
#include <Wire.h>
#include <SPI.h>

Adafruit_PCD8544 display = Adafruit_PCD8544(8, 9, 10, 11, 12);
Adafruit_BMP085 bmp180;
DHT dht(3, DHT22);


void setup() {

  Serial.begin(9600);

  display.begin();
  display.setContrast(48);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(BLACK);
  
  
  dht.begin();
  bmp180.begin();
  
  display.setCursor(1,1);  // sets cursor position
  display.println("TEMP"); // print
  display.setCursor(30,1); 
  display.println("---  C"); 

  display.setCursor(1,12);  
  display.println("UMID");
  display.setCursor(30,12); 
  display.println("---  %"); 


  display.setCursor(1,23); 
  display.println("PRES");
  display.setCursor(30,23); 
  display.println("------ mB"); 
}

void loop() {

  float p = bmp180.readPressure()/100.0;
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  
//   if (isnan(h) || isnan(t)) {
//    Serial.println("Failed to read from DHT sensor!");
//   return;
//   }

  display.fillRect(29,1,24,10,0); //draws a white rectangle above the "---"'s done in void  setup
  display.setCursor(29,1);        //sets cursor above the white rectangle
  display.println(t,1); // prints the temperature with 1 decimal 

  display.fillRect(29,12,24,10,0);
  display.setCursor(29,12);
  display.println(h,1); 

  display.fillRect(29,23,42,10,0);
  display.setCursor(29,23);
  display.println(p,2); 

  display.display();

  delay(2000);


//  Serial.print("Humidity: "); 
//  Serial.print(h);
// Serial.print(" %\t");
//  Serial.print("Temperature: "); 
//  Serial.print(t);
//  Serial.print(" *C ");
//  Serial.print("Pressure: ");
//  Serial.print(p);
//  Serial.println(" mb");
}

I used the commented serial-code-parts to see if it wasn't the display being buggy, but it isn't.

@Robin2
The 2 options that work (1 sensor + display) can be done by simply commenting the "other sensor" code parts.
When I try to run it all together I receive 0 / n.a.n on the humidity sensor.

I hope it's all clear. If any more information is needed, i'll be glad to give it.

I'm powering all of it through an USB port.

Power into the Leonardo via USB? Then power to LCD and sensors from 5V pin of Leonardo?

Possibly too much current drawn. It was this that made me think about it ...

LCD presents some fading out.

What happens if you remove power to the LCD and try the two sensors at the same time? Do they work OK?

Yes, PC -> Leonardo -> Sensors/LCD for the voltage.
If I remove the LCD and try the two sensors at the same time, I still get 0/n.a.n..
What could possibly make the sensors conflict with one another?
Since one communicates with I²C and the other with one-wire....

Looking at the Leonardo page on this website, plus a couple of threads, it looks like the I2C pins are also connected to digital pins 2 and 3. So definitely worth moving the other sensor to a different pin.

Hey, I saw that.
I tried to put the sensor in many digital pins: 4, 7, 8... and none of them worked.
I just tried the same circuit with an Arduino Nano, and it worked perfectly.
So it must be some conflict between pins in Leonardo and the communication protocols of the sensors.
I just can't figure out what :confused:

Last thing I can think of is to try using the hardware SPI pins on the ICSP connector on the Leonardo. At the moment, specifying five pins in the constructor tells the Adafruit library to use software SPI.

Can you post a link to the LCD module you are using?

Hackscribble:
Last thing I can think of is to try using the hardware SPI pins on the ICSP connector on the Leonardo. At the moment, specifying five pins in the constructor tells the Adafruit library to use software SPI.

Can you post a link to the LCD module you are using?

Just search eBay or alien press for one, the Nokia 5110's are all the same, only differences are the names of some pins for example 5V instead of Vcc and the color.

If I'd use the ICSP header, how would this code line become?

Adafruit_PCD8544 display = Adafruit_PCD8544(8, 9, 10, 11, 12);

The Adafruit constructor for hardware SPI is:

  // Hardware SPI based on hardware controlled SCK (SCLK) and MOSI (DIN) pins. CS is still controlled by any IO pin.
  // NOTE: MISO and SS will be set as an input and output respectively, so be careful sharing those pins!
  Adafruit_PCD8544(int8_t DC, int8_t CS, int8_t RST);

So connections would be as follows, plus power:

1.RST--------- reset = pin 12
2.CE------------chip selection = pin 11
3.DC-----------data/commands choice = pin 10
4.DIN-----------serial data line = ICSP-4
5.CLK------------serial Clock Speed = ICSP-3

The first three are your current pin assignments, I believe.

The declaration would be:

Adafruit_PCD8544 display = Adafruit_PCD8544(10, 11, 12);

pedroseger:
@Robin2
The 2 options that work (1 sensor + display) can be done by simply commenting the "other sensor" code parts.
When I try to run it all together I receive 0 / n.a.n on the humidity sensor.

Does that mean that all the hardware remains connected, and you only change the code ?

...R

I change the code and disconnect the sensor I'm not using. The other parts of the hardware remain the same.

Hey there.
It now seems to be working correctly.
I wired the SPI pins just as Hackscribble mentioned, and my humidity sensor to D7.
I can conclude the following:

  • D3 can't be used for the one-wire since it's used for SDA/SCL;
  • SPI must be wired to the ICSP header.
    Is that correct?
    Thank you all for your support!

That's good news :slight_smile:

Yes, there seems to be some clash between software SPI (using pins 8 and 9) and the sensors.

pedroseger:
I change the code and disconnect the sensor I'm not using. The other parts of the hardware remain the same.

Glad you have it working.

For the future, when debugging you should only make one change at a time - in this case either change the code, or disconnect the sensor, but not both - just in case it is an electrical problem, rather than a code problem.

...R