Hello,
I am trying to build a system for a bottle rocket which automatically deploys a parachute using a servo depending on altitude data received by the barometric pressure sensor (MPL3115a2).
Here is my code:
#include <I2C.h>
#include <Wire.h>
#include "SparkFunMPL3115A2.h"
#include <Servo.h>
//Create an instance of the object
MPL3115A2 myPressure;
//Define the led pin
int led = 9;
//Define servo pin
int servoPin = 5;
//Create servo object
Servo myServo;
//Define floats
float curAlt;
float prevAlt;
void setup()
{
Wire.begin(); // Join i2c bus
Serial.begin(9600); // Start serial for output
myPressure.begin(); // Get sensor online
//Configure the sensor
myPressure.setModeAltimeter(); // Measure altitude above sea level in meters
//myPressure.setModeBarometer(); // Measure pressure in Pascals from 20 to 110 kPa
myPressure.setOversampleRate(7); // Set Oversample to the recommended 128
myPressure.enableEventFlags(); // Enable all three pressure and temp event flags
//test LED
pinMode(led, OUTPUT);
//Attach servo
myServo.attach(servoPin);
myServo.write(0);
}
void loop()
{
float altitude = myPressure.readAltitude(); //get the value of the sensor
prevAlt = curAlt; //change previous altitude to current altitude
curAlt = altitude; //before changing current altitude to the (current) altitude sensor reading
//print all values
Serial.print(" Altitude(M):");
Serial.print(altitude);
Serial.print(" ");
Serial.print("PrevAlt:");
Serial.print(prevAlt);
Serial.print(" ");
Serial.print("CurAlt:");
Serial.print(curAlt);
Serial.println();
//this is only for use later in my project (when the rocket falls, activate something)
if (curAlt - prevAlt < -0.7) {
myServo.write(180);
digitalWrite(led, HIGH);
delay(5000);
digitalWrite(led, LOW);
myServo.write(0);
}
delay(500);
}
The whole system works as expected when connected to the USB terminal and the Macbook but as soon as I connect it to the 9v battery it has a problem. It work normally until the servo is activated. As seen in the code, the servo is supposed to rotate 180, wait 5 seconds and then turn back. It does so when connected to the computer but when it is connected to the battery, it only turns 180 but does not turn back and, as far as I know, also does not read any values. I then have to re-upload the program and then can do it again.
"DC 9v battery"
Not a PP3 battery by any chance ?
If so, they are designed for low current applications such as smoke detectors and cannot supply the required current for very long to run an Arduino let alone a servo. Also, you should not be powering your servo through the Arduino anyway as it is not designed to be used as a power supply
I have tried different companies but that did not make a difference. I don't think the problem is that I am powering the servo through the Arduino because all tutorials do this and it works when connected to the computer via USB.
Regarding the battery, should I be using a different battery? Which one? What connector?
Use a LiPo and a boost converter to get 5V and feed that to the 5V pin on the Arduino
As to
Tutorials need to be followed with caution
The problem is that a servo takes a large stall current when starting, or when stalled, of course. Add to that the fact that some servos take more current than others and someone will connect multiple servos and you can probably see why the best advice is not to power servos from the Arduino
Your use case may be OK but take care and if possible, don't do t
Hi,
thanks for your help. I would order this Boost Converter and this LiPo battery.
Is this correct for my application and is there anything else I should think about?
Hello, I am not good at this things, and I don't believe this is the case for you, but it happened a while ago where my code didn't work well and which actually appeared that the "my battery didn't work well" because weren't charge enough. Just make sure your battery is charge enough, or also they might be damaged in another way so try another battery if possible. Hope this helps.
Hi, thanks for your reply. Until now, I have been using non-rechargeable batteries so I don't think that is the case however, I might try buy a new one.
Thanks anyways
Nick
Hi everyone, the solution is as follows:
I connected the 9v battery to Vin and GND to power the arduino. I then used a small 3.7v LiPo which is connect directly to the Power and GND of the servo. This GND wire also connects to the other GND of the arduino and the signal wire also connects to a digital port.
I hope this helps
So, as expected, the problem was that the PP3 battery could not supply enough current to run the servo. Do not expect it to run the Nano for very long either