Go Down

Topic: BMP280 & arduino nano, only works when connected to USB (Read 215 times) previous topic - next topic

marpbcn

Hello,

I have designed a system to trigger a parachute in a water rocket based on the BMP280 pressure sensor, I measure the instant height and when rocket starts to descend the system moves a servo to trigger the parachute.

I have very limited experience with arduino and is the first time I use I2C communication.

The system works perfect when arduino is switched on using USB cable. If I feed the arduino nano with an external battery the servo moves to the parachute trigger position and remains there.

If I switch on (feed) the arduino using the usb cable, connect the external battery while connected to the USB cable and then disconnect the USB cable, the system continues working well.

Down here is the code, this is a short version, I have a longer one that allows me to monitor via usb the different height values, and its behaviour is the same.

How can make this system work feeded directly from the onboard battery and without connecting it first to the USB port?


The library I have used for the BMP280 sensor is from:

https://github.com/adafruit/Adafruit_BMP280_Library


Code: [Select]


/***************************************************************************
 Water Rocket parachute deploiment software for arduino nano written by Miquel Àngel Rodríguez
 
 
  It uses the library for the BMP280 humidity, temperature & pressure sensor


  These sensors use I2C or SPI to communicate, 2 or 4 pins are required
  to interface.


  This sketch uses parts of the sketch Written by Limor Fried & Kevin Townsend for Adafruit Industries. 
  BSD license.
 ***************************************************************************/
#include <Servo.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>

#define BMP_SCK 13
#define BMP_MISO 12
#define BMP_MOSI 11
#define BMP_CS 10

Servo myservo;  // creates servo object

const int buttonPin = 2;     // the number of the pushbutton pin
 
int pos = 0;    // posicion del servo
int AlcadaInicial = 0; //initial height above mean sea level/ alçada inicial respecte al nivell del mar
int AlcadaMax = 0; //max height achieved above mean sea level/ alçada maxima assolida respecte al nivell del mar
int AlcadaInst = 0; //instant height above mean sea level/ alçada instantanea respecte al nivell del mar
int AlcadaTerra = 0; //height above ground calculated using the initial ground height/ alçada respecte al terra calculant amb l'alçada inicial
int AlcadaTerraMax = 0; //Maximum height achieved above initial ground height/ alçada Max respecte al terra calculant amb l'alçada inicial
int Llindar = 0; //the difference in height that triggers the parachute when descending / Diferencial d'alçada que fa activar el servo que allibera el paracaigudes
int buttonState = 0; // variable for reading the pushbutton status



Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
//Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);

void setup() {
  pinMode(buttonPin, INPUT);   // initialize the pushbutton pin as an input:
  myservo.attach(5);  // links servo to pin #5 / vincula el servo al pin digital 5
  !bmp.begin();
  AlcadaInicial = bmp.readAltitude(1013.25); //reads the ground height above mean sea level
  myservo.write(30); //moves servo to the "close" position.
}

void loop() {

   buttonState = digitalRead(buttonPin);  // read the state of the pushbutton value
   if (buttonState == HIGH) {
   myservo.write(170); // if pushbutton is pressed trigger the parachute
   }
   else {
    myservo.write(30); // otherwise leave Close the servo hook
   }
   
    AlcadaInst = bmp.readAltitude(1013,25); //reads current height above mean sea level
    if (AlcadaInst > AlcadaMax){//check if current height is higher than the max height registered
      AlcadaMax = AlcadaInst; // update maximum height above mean sea level achieved if necessary
    }
   
    AlcadaTerra = bmp.readAltitude(1013.25)- AlcadaInicial; //calculates the height above the ground
    AlcadaTerraMax = AlcadaMax - AlcadaInicial; //calculates the the maximum height above the ground achieved
    Llindar = AlcadaMax - AlcadaInst; // calculates the difference between the max height above mean sea level and the current one
       

    if (Llindar > 1){ // check if the rocket starts to descend
      myservo.write(170); //trigger the parachute
    }

 
   
    delay(1000);
}


Regards,

Miquel Àngel

pylon

This doesn't sound like a software problem to me. I guess there is a problem with the hardware. Please provide links to all hardware you use and post a wiring diagram that shows all connections between the components.

marpbcn

Hello Pylon,

Thank you for your replay.

The arduino and the BMP280 are cheap copies from Banggood:

http://ves.cat/emFx
http://ves.cat/emFy

The wiring diagram is the one on the attached image.


hanslanda

I personally just setup a BMP280 last night. I used all adafruit gear and I had a hell of a time using I2C. Now I was using a adafruit feather, which could be why, because the wiring diagram shows a need for pull-up resistors on the I2C buss.

For me, I couldn't get the sensor to read anything with a 4.7K pull-up but I know jack shit about how pull-ups should be. In any case with NO pull-up the sensor worked on I2C. BUT after about 5 minutes of monitoring the data I noticed the readings were becoming more and more garbled and after a while the sensor wouldn't work at all.

My solution was hardware SPI. After some monitoring on spi, the sensor works great, and the communication is faster. Go with that if you can.

marpbcn

Thank you for your answer hanslanda, I have tried putting a 1K resistor between SCL and VCC and SDA and VCC and that has not solved the problem.

Now I was trying to connect the BMP280 to the arduino NANO using SPI bus but I do not find the equivalence between SCLK, MISO, MOSI, SS and the ones on the BMP280 sensor SCL, SDA, CSB and SDD, can you attache a wiring diagram?

Thank you

pylon

Quote
Thank you for your answer hanslanda, I have tried putting a 1K resistor between SCL and VCC and SDA and VCC and that has not solved the problem.
1kΩ is to weak, you may damage the sensor. Your breakout board already includes pull-ups so you don't have to add yourself.

Quote
Now I was trying to connect the BMP280 to the arduino NANO using SPI bus but I do not find the equivalence between SCLK, MISO, MOSI, SS and the ones on the BMP280 sensor SCL, SDA, CSB and SDD, can you attache a wiring diagram?
If I interpret the pictures correctly, this board is configured to be used with I2C (CS is pulled high). To be used with SPI you need a level converter, otherwise you fry your sensor (it's not 5V tolerant).

What kind of battery are you using? I guess the initial current for the servo is to high and the voltage drops to low if you start with the battery only.

hanslanda

1kΩ is to weak, you may damage the sensor. Your breakout board already includes pull-ups so you don't have to add yourself.

If I interpret the pictures correctly, this board is configured to be used with I2C (CS is pulled high). To be used with SPI you need a level converter, otherwise you fry your sensor (it's not 5V tolerant).

What kind of battery are you using? I guess the initial current for the servo is to high and the voltage drops to low if you start with the battery only.
1) Look at what i said. I ran this sensor on I2C with no pull-ups. It didn't go well.


2)BMP280 description:  "comes with a 3.3V regulator and level shifting so you can use it with a 3V or 5V logic microcontroller without worry".... so yeah he can try hardware SPI.


I think this is the wiring i went with, ill check when i get home though:


{bmp280 - ardu)

vin          -   LogiclevelV)

gn          -     gnd

sck         -     sck

sdi          -    mosi

sdo        -     miso

cs          -     any digital in







pylon

Quote
1) Look at what i said. I ran this sensor on I2C with no pull-ups. It didn't go well.
Did you use the same breakout board as the OP?

Quote
2)BMP280 description:  "comes with a 3.3V regulator and level shifting so you can use it with a 3V or 5V logic microcontroller without worry".... so yeah he can try hardware SPI.
The board the OP uses doesn't come with a 3V3 regulator and has no level shifting. So no, he cannot try without a level converter. The sensor datasheet clearly states that the maximum voltage on the interface pins is Vddio + 0.3V. A 5V level will probably damage the chip.

Check the links the OP provided and don't insist in suggesting things that may damage his hardware!

hanslanda

Did you use the same breakout board as the OP?

The board the OP uses doesn't come with a 3V3 regulator and has no level shifting. So no, he cannot try without a level converter. The sensor datasheet clearly states that the maximum voltage on the interface pins is Vddio + 0.3V. A 5V level will probably damage the chip.

Check the links the OP provided and don't insist in suggesting things that may damage his hardware!
Oh jesus christ one of these.


HIS schematic shows ADAFRUIT BMP280 right on it. So I didn't care to look at his datasheet considering I have the exact same sensor and, as described, successfully interfaced with it (IE. I know the datasheet).



Ditch the bangood junk and get the adafruit version... unless you want to wire in a lls like mentioned, personally i feel that introduces some variables you could avoid by getting the better breakout.


https://www.amazon.com/Adafruit-BMP280-Barometric-Pressure-Altitude/dp/B013W0RR6Y/ref=sr_1_1_sspa?ie=UTF8&qid=1508183941&sr=8-1-spons&keywords=adafruit+bmp280&psc=1

$10 and One-day shipping depending on where you live.



Batman_429

BME280 is a amazing sensor but it has so many registers which needs to set before you can use it reliably, You might want to look into a reliable supplier.
I use this lib to read the BME280 sensor with arduino Library
the board which is used with this library is this one BME280 plug and play
its real simple to use, it has on board level shifter  so you can use with 5V arduino as well.


Go Up