Controlling 2 servo motors with BMP180

Hello everyone,

I am completely new in this Arduino environment and I have a project where I want to control 2 servo motors with data received from the BMP180 pressure sensor.

I have two separate codes. One of them is for BMP180 and the other one is combined code which creates the link between servos and pressure sensor. If I upload only the code of BMP180, it works. However, when I upload the combined code, neither the pressure sensor nor servos don't react.

I have found a similar code on the internet for the combined code. It was for a motion sensor. I adapted it with the pressure sensor. I have no idea if it is fully correct. :sweat_smile:

You may find the codes below.

Thanks in advance for your help :blush:

BMP180

#include <SFE_BMP180.h>
#include <Wire.h>

SFE_BMP180 pressure;

#define ALTITUDE 30.0 // Altitude of Izmir in meters

void setup()
{
  Serial.begin(9600);
  Serial.println("REBOOT");

  
  if (pressure.begin())
    Serial.println("BMP180 init success");
  else
  {
    Serial.println("BMP180 init fail\n\n");
    while(1);
  }
}

void loop()
{
  char status;
  double T,P,p0,a;

  Serial.println();
  Serial.print("provided altitude: ");
  Serial.print(ALTITUDE,0);
  Serial.print(" meters, ");
    

  status = pressure.startTemperature();
  if (status != 0)
  {
    delay(status);

    status = pressure.getTemperature(T);
    if (status != 0)
    {
      Serial.print("temperature: ");
      Serial.print(T,2);
      Serial.print(" deg C, ");
      
     status = pressure.startPressure(3);
      if (status != 0)
      {
        delay(status);

        status = pressure.getPressure(P,T);
        if (status != 0)
        {
          Serial.print("absolute pressure: ");
          Serial.print(P,2);
          Serial.print(" mb, ");
          Serial.print(P*100);
          Serial.println(" Pa");

          p0 = pressure.sealevel(P,ALTITUDE); // we're at 1655 meters (Boulder, CO)
          Serial.print("relative (sea-level) pressure: ");
          Serial.print(p0,2);
          Serial.print(" mb, ");
          Serial.print(p0*100);
          Serial.println(" Pa");

          a = pressure.altitude(P,p0);
          Serial.print("computed altitude: ");
          Serial.print(a,0);
          Serial.print(" meters, ");
          Serial.print(a*3.28084,0);
          Serial.println(" feet");
        }
        else Serial.println("error retrieving pressure measurement\n");
      }
      else Serial.println("error starting pressure measurement\n");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
  else Serial.println("error starting temperature measurement\n");

  delay(5000);  // Pause for 5 seconds.
}

Combined code

#include <SFE_BMP180.h>
#include <Wire.h>
#include <Servo.h>


int bmp = A6;                   //Sensörü takacağımız pin
int servoPin = A7;                 //Servoyu takacağımız pin
int basinc;                      //Sensörden aldığımız veri
int pos = 0;

Servo motor;                      //Servo motor değişkeni


void setup() {
  motor.attach(servoPin);         //Servomuzu 9. pin ile ilişkilendiriyoruz.
  pinMode(bmp, INPUT);         //Sensör pinimizi giriş olarak ayarlyoruz.

}

void loop() {
  basinc = digitalRead(A6);  //Sensörden okuma yapıyoruz.

  if (basinc<=10000) {          //Hareketlilik var ise içerideki komutlar uygulanır.
    for (pos = 0; pos <= 5; pos += 1)
    
    motor.write(pos);           
    delay(250);
  }

  if (basinc>10000)  {
    for (pos = 5; pos >= 0; pos -= 1)
    motor.write(pos)
;  }
  else {                          //Hareketlilik yok ise içerideki komutlar uygulanır.
    motor.write(90);

  }
}

Both HIGH and LOW are comfortably less than 10000

The measured pressure value is as follows:

I wrote the pressure range in accordance.

See reply #2

Thank you for your answer but to be honest, I did not understand what you mean :slightly_frowning_face:

You're doing a digitalRead of pin A6.
I have no idea what you have connected to pin A6, but digitalRead can return only HIGH (aka 1) or LOW (aka 0).

Even if you did an analogRead of A6, the maximum value it could return would be 1023, which is still less than 10000

What Arduino are you using? Why are you using pins A6 and A7 ?

Steve

I changed the code as below. Still not working.

#include <SFE_BMP180.h>
#include <Wire.h>
#include <Servo.h>


int bmp = A5;                   //Sensörü takacağımız pin
int servoPin = A7;                 //Servoyu takacağımız pin
int basinc;                      //Sensörden aldığımız veri
int pos = 0;

Servo motor;                      //Servo motor değişkeni


void setup() {
  motor.attach(servoPin);         //Servomuzu 9. pin ile ilişkilendiriyoruz.
  pinMode(bmp, INPUT);         //Sensör pinimizi giriş olarak ayarlyoruz.

}

void loop() {
  basinc = analogRead(A5);  //Sensörden okuma yapıyoruz.

  if (basinc<=1023) {          //Hareketlilik var ise içerideki komutlar uygulanır.
    for (pos = 0; pos <= 5; pos += 1)
    
    motor.write(pos);           
    delay(250);
  }

  if (basinc>1023)  {
    for (pos = 5; pos >= 0; pos -= 1)
    motor.write(pos)
;  }
  else {                          //Hareketlilik yok ise içerideki komutlar uygulanır.
    motor.write(90);

  }
}```

What is connected to pin A5?

I am using Arduino Nano.

There is no specific reason. My cabling is as follows:

BMP180

  • SDA --> A4
  • SCL --> A5

Servo --> A7

I changed the code as reply #8.

BMP180

  • SDA --> A4
  • SCL --> A5

Servo --> A7

Stick with I2C.
It works.

I will search, but in parallel could you please give me more detail if possible?

Merci beaucoup. :blush:

Look at your original code.
It uses a library to read temperature and pressure from an I2C device into variables.
Variables which could be tested with an "if"

Start there.

De rien.

A6 and A7 on a Nano are analog input pins only. They cannot be used as digital pins.

Steve

The last code that I have now is as follows:

#include <SFE_BMP180.h>
#include <Wire.h>
#include <Servo.h>


int servoPin = A7;                 //Servoyu takacağımız pin
int absolute_pressure;             //Sensörden aldığımız veri
int pos = 0;

Servo motor;                      //Servo motor değişkeni
SFE_BMP180 pressure;

#define ALTITUDE 30.0 // Altitude of Izmir in meters

void setup() {
  motor.attach(servoPin);         //Servomuzu 9. pin ile ilişkilendiriyoruz.
  pinMode(absolute_pressure, INPUT);         //Sensör pinimizi giriş olarak ayarlyoruz.
  pinMode(servoPin, OUTPUT);

  Serial.begin(9600);
  Serial.println("REBOOT");

  
  if (pressure.begin())
    Serial.println("BMP180 init success");
  else
  {
    Serial.println("BMP180 init fail\n\n");
    while(1);
  }
}

void loop() {
  absolute_pressure = analogRead(A4);  //Sensörden okuma yapıyoruz.

  if (absolute_pressure<=1000) {          //Hareketlilik var ise içerideki komutlar uygulanır.
    for (pos = 0; pos <= 5; pos += 1)
    
    motor.write(pos);           
    delay(250);
  }

  if (absolute_pressure>1000)  {
    for (pos = 5; pos >= 0; pos -= 1)
    motor.write(pos)
;  }
  else {                          //Hareketlilik yok ise içerideki komutlar uygulanır.
    motor.write(90);

  }

  {
  char status;
  double T,P,p0,a;

  Serial.println();
  Serial.print("provided altitude: ");
  Serial.print(ALTITUDE,0);
  Serial.print(" meters, ");
    

  status = pressure.startTemperature();
  if (status != 0)
  {
    delay(status);

    status = pressure.getTemperature(T);
    if (status != 0)
    {
      Serial.print("temperature: ");
      Serial.print(T,2);
      Serial.print(" deg C, ");
      
     status = pressure.startPressure(3);
      if (status != 0)
      {
        delay(status);

        status = pressure.getPressure(P,T);
        if (status != 0)
        {
          Serial.print("absolute pressure: ");
          Serial.print(P,2);
          Serial.print(" mb, ");
          Serial.print(P*100);
          Serial.println(" Pa");

        }
        else Serial.println("error retrieving pressure measurement\n");
      }
      else Serial.println("error starting pressure measurement\n");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
  else Serial.println("error starting temperature measurement\n");

  delay(5000);  // Pause for 5 seconds.
}
}

No. You already had code to read pressure in your original code. You cannot use analogRead on an I2C device.

No, you've been told A7 cannot be used as a digital pin.

I translated the comments into English. It would be better for you to understand.

I will start running the code with only one servo at first. I have a servo, a BMP180 pressure sensor, Arduino Nano. My target is to create a link between sensor and servo. If the absolute pressure received from the sensor is higher than 1000 millibars, the servo will turn clockwise to 5 degrees. If the pressure is lower than 1000 millibars, the servo will turn counterclockwise to 5 degrees. How can I write this code?

#include <SFE_BMP180.h>
#include <Wire.h>
#include <Servo.h>


int servoPin = A7;                 //Pin on which we will attach the servo
int absolute_pressure;             //The data we receive from the sensor
int pos = 0;

Servo motor;                      //Servo motor variant
SFE_BMP180 pressure;              ////Pressure sensor variant

#define ALTITUDE 30.0 // Altitude of Izmir in meters

void setup() {
  motor.attach(servoPin);                    //We associate our servo with pin 9.
  pinMode(absolute_pressure, INPUT);         //We set our sensor pin as input.
  pinMode(servoPin, OUTPUT);                 //We set our servo pin as input.

  Serial.begin(9600);
  Serial.println("REBOOT");

  
  if (pressure.begin())
    Serial.println("BMP180 init success");
  else
  {
    Serial.println("BMP180 init fail\n\n");
    while(1);
  }
}

void loop() {
  absolute_pressure = analogRead(A4);  //We are reading from the sensor.

  if (absolute_pressure<=1000) {          //If the pressure is less than or equal to 1000 mb, the command inside are executed.
    for (pos = 0; pos <= 5; pos += 1)
    
    motor.write(pos);           
    delay(250);
  }

  if (absolute_pressure>1000)  {
    for (pos = 5; pos >= 0; pos -= 1)     //If the pressure is more than 1000 mb, the command inside are executed.
    motor.write(pos)
;  }
  else {                          
    motor.write(90);

  }

  {
  char status;
  double T,P,p0,a;

  Serial.println();
  Serial.print("provided altitude: ");
  Serial.print(ALTITUDE,0);
  Serial.print(" meters, ");
    

  status = pressure.startTemperature();
  if (status != 0)
  {
    delay(status);

    status = pressure.getTemperature(T);
    if (status != 0)
    {
      Serial.print("temperature: ");
      Serial.print(T,2);
      Serial.print(" deg C, ");
      
     status = pressure.startPressure(3);
      if (status != 0)
      {
        delay(status);

        status = pressure.getPressure(P,T);
        if (status != 0)
        {
          Serial.print("absolute pressure: ");
          Serial.print(P,2);
          Serial.print(" mb, ");
          Serial.print(P*100);
          Serial.println(" Pa");

        }
        else Serial.println("error retrieving pressure measurement\n");
      }
      else Serial.println("error starting pressure measurement\n");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
  else Serial.println("error starting temperature measurement\n");

  delay(5000);  // Pause for 5 seconds.
}
}

I'm not the least bit interested in the comments.
You have an I2C device attached to pin A4 (and A5)
You CANNOT read that device using analogRead.

You have a digital device (a servo) attached to pin A7.
Pin A7 CANNOT be used to control a digital device.

Ok, I have fixed this. Now servo is working but not according to the pressure value. Sensor is working as well. I get the pressure values properly but there is no connection in between.

What should I do to fix this if I cannot use analogRead? I changed the pins from A4-A5 to D4-D5 and I wrote digitalRead instead of analogRead. It says something like "D4 is not declared."