0

I am successfully using a BMP280 sensor via SPI connection with an Arduino Uno R3 board. I'm trying to run the same code with the same setup on an Arduino Ethernet. However the sensor is not initializing.

The setup function looks as follows:

void setup() {
    delay(1000);        
    dht.begin(); // starts DHT sensor
    chk = dht.read(DHTPIN); // reads DHT status
    while ((!bmp.begin()) || (chk != 1) ) {
      // checks DHT and BMP status and shows red LED, in case one of them
      // wasn't found. Loops and resets timeout each time.
      wdt_reset();
      Serial.println("Could not find a valid sensor, check wiring!");
      digitalWrite(RED, HIGH);
      delay(300);
      digitalWrite(RED, LOW);
      delay(1000);
      chk = dht.read(DHTPIN);
    }
}

The code only returns the error message and does not initialize the BMP sensor (DHT is fine).

Full code:

#include "ThingSpeak.h"    
#include <SPI.h>
#include <DHT.h>
#include <Ethernet.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xE5, 0xBF};
IPAddress ip(10,42,0,69);
EthernetClient client;

// --- Libraries --- //
#include <avr/wdt.h> // Watchdog, interrupts and reboots
#include <Adafruit_BMP280.h> // BMP library for BMP280

// --- Constants --- //
// DHT22
#define DHTPIN 7
// BMP280 (SPI)
#define BMP_SCK 13 // Serial clock pin for BMP (10k pullup to +5V)
#define BMP_MISO 12 // SDO, has 10k pullup
#define BMP_MOSI 11 // SDI, no pullup
#define BMP_CS 9 // CS, has 10k pullup
// LEDs
#define RED 5 // pin for LEDs
#define YELLOW 3
#define GREEN 2

unsigned long myChannelNumber = 115634;
const char * myWriteAPIKey = "***"; // removed for privacy reasons
float dht_temp;
float dht_hum;
float bmp_temp; // values from sensors
float bmp_press;
int chk; // DHT status (0 or 1)
int k; // counter variable
int number;
char COLOR;

// --- Initialization --- //
DHT dht(DHTPIN, DHT22); // initializes sensors
Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);

void setup() {
  wdt_disable(); // disables watchdog for initial communication
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);
  pinMode(YELLOW, OUTPUT); // LED pins set as output
  pinMode(RED, OUTPUT);
  digitalWrite(YELLOW, HIGH); // starts yellow LED
  Serial.begin(9600); // begin serial connection with 9600 baud
  delay(500); // delay to get connection and sensors ready
  wdt_enable(WDTO_8S); // start watchdog timeout (4 sec might be too short)
  blink(RED, 3);
  Serial.println(bmp.begin());
  dht.begin(); // starts DHT sensor
  chk = dht.read(DHTPIN); // reads DHT status
  if ((!bmp.begin()) || (chk != 1) ) {
    // checks DHT and BMP status and shows red LED, in case one of them
    // wasn't found. Loops and resets timeout each time.
    wdt_reset();
    Serial.println("Could not find a valid sensor, check wiring!");
    digitalWrite(RED, HIGH);
    delay(300);
    digitalWrite(RED, LOW);
    delay(1000);
    chk = dht.read(DHTPIN);
  }
  Ethernet.begin(mac, ip);
  ThingSpeak.begin(client);
}

// --- Loop --- //
void loop() {
  wdt_reset(); // resets watchdog
  pinMode(GREEN, OUTPUT); // gets green LED ready
  digitalWrite(GREEN, LOW);
  delay(1600); // delay for sensors to be read. In total, at least 2 sec!
  dhtread();
  delay(5000);
  wdt_reset();
  bmpread();
  ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
  delay(5000);
  wdt_reset();
  delay(5000);
}

void blink(char COLOR, int number) {
  for (k = 1; k <= number; k = k + 1) {
    // blinks 3x to show that Arduino rebooted
    digitalWrite(COLOR, HIGH);
    delay(100);
    digitalWrite(COLOR, LOW);
    delay(100);
  }
}

void bmpread() {
  bmp_temp = bmp.readTemperature(); // reads BMP sensor
  bmp_press = bmp.readPressure();
  Serial.println(bmp_temp);
  Serial.println(bmp_press);
  if (bmp_press > 0.0) {
    // if sensor isn't attached, it shows 0.0 or negative pressure.
    // This is used as check criterion
    digitalWrite(GREEN, HIGH); // blinks green for good BMP values
    Serial.println("bmp ok");
    ThingSpeak.setField(3,bmp_temp);
    ThingSpeak.setField(4,bmp_press);
    delay(150);
    digitalWrite(GREEN, LOW);
  } else {
    // if pressure is 0.0 or negative, blink red
    // and print 0.0 to keep formatting
    digitalWrite(RED, HIGH);
    Serial.println("Error, BMP280 missing");
    delay(150);
    digitalWrite(RED, LOW);
  }
}

void dhtread() {
  chk = dht.read(DHTPIN); // checks if connection to DHT sensor is ok (0: no connection, 1: ok)
  if (chk == 1) {
    dht_temp = dht.readTemperature();
    dht_hum = dht.readHumidity(); // reads DHT sensor
    digitalWrite(GREEN, HIGH); // blinks once for successful DHT sensor values
    ThingSpeak.setField(1,dht_temp);
    ThingSpeak.setField(2,dht_hum);
    delay(150);
    digitalWrite(GREEN, LOW);
    delay(100);
  } else {
    // if no connection, blink red instead and print "nan"
    // (keeps formatting ok)
    digitalWrite(RED, HIGH);
    Serial.print("Error, DHT22 missing");
    delay(150);
    digitalWrite(RED, LOW);
    delay(100);
  }
}

The problem now is that it seems to initialize (bmp.begin() returns 1), but the sensor is still not reading a correct value (either 0.0 for both pressure and temperature, or a large negative pressure and very large temperature (-2602 Pa, 185°C). What might be the problem? I have tried it with another sensor of the same type, same result. Where else can I attach the CS wire to?

enter image description here

  • To what pins did you connect the BMP280? It could be that you are using a pin that the Ethernet module already uses. I suspect you are using Pin 10 as the chip-select for the BMP. – Gerben May 12 '16 at 12:58
  • Ah no, I am using an Arduino Ethernet, that has the Ethernet module built in (not a shield). I am using 13 for SCK, 12 for MISO, 11 for MOSI and 10 for CS (for CS Ive tried the other free pins I have, 9, 8 and 7). – ahemmetter May 12 '16 at 13:04
  • There is also a DHT22 sensor connected to it (on pin 6), which works fine: thingspeak.com/channels/115634 The bottom two fields show the BMP280 values (it returns 0.0 if it isnt connected) – ahemmetter May 12 '16 at 13:06
  • As an update, I managed (somehow) to initialize the sensor by putting the CS on 7 (10 also works though) and disabling the SD card by pulling pin 4 high as output. It still fails to read the correct value though (and returns 0). Any other ideas? – ahemmetter May 12 '16 at 13:47
  • 1
    You can only enable one 'chip-select' (CS) line at a time, that's how the devices know who you're talking to. You say you got something to work by disabling the SD card... that would be your issue then... You're selecting the SD Card AND the BMP280 at the same time, they're both talking on the SPI bus. – lornix May 13 '16 at 23:04
1

The software must be OK, because it runs on the other board, so it has to be a hardware fault. The sensor is OK, because it works on the other board. Assuming the board isn't defective then it must be the way you have connected it.

You haven't included your pin designations. Some Ethernet shields use certain pins and prevent them being used by other devices. Is it possible that this could be the cause of you problems?

  • Documentation says to use pins 13-10 for SPI connections. This is how I defined the pins: #define BMP_SCK 13 (and the others in the same way), and later initialized the sensor with Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK); which worked fine on the Uno board. – ahemmetter May 12 '16 at 12:36
  • From Arduino.cc - "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." I don't know if this applies to you board. – Code Gorilla May 12 '16 at 12:40
  • 1
    I think you will need to change BMP_CS pin, but not to 4 because that's used by the SD card. – Code Gorilla May 12 '16 at 12:41
  • I changed it to 8 and 9, both seems to have the same result – ahemmetter May 12 '16 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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