Servo not working when connected to battery

Dear Arduino Forum,
I am try to build a bottle rocket which automatically deploys a parachute using a servo motor when a pressure sensor(MPL3115a2) detects decreasing altitude. I have figured out how to make everything work when connected via USB Mini B to the PC. (I am using arduino Nano) This is the code which works:

#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);

}

This code works when connected to my Macbook and I have been able to activate the sensor and make it turn back. As soon as I connect a 9v battery to GND and Vin it does not work anymore (only twitching slightly) I have written this code to test the servo:

#include<Servo.h>

Servo myServo;

int pos = 0;


void setup() {


  myServo.attach(5);

Serial.begin(9600);
}

void loop() {

  myServo.write(pos);

  if (pos == 0) {
    pos = 180;
    myServo.write(pos);
    delay (1000);

  }
 Serial.println(pos);
  if (pos == 180) {
    pos = 0;
    myServo.write(pos);
    delay (1000);

  }

  Serial.println(pos);

}

Now to the things I have tried:

  • I have powered arduino using 9v battery and power servo using 5+ arduino

  • I have bought a 3.7v LiPo and had it power the arduino and servo through 5+ from arduino

  • I have used a step up converter together with the 3.7v LiPo (arduino started smoking/was fried and I changed all the parts involved)

  • I have connected 9v battery to power arduino and 3.7v LiPo separately to power servo (so that it does not take too much power and stall the arduino)

  • I have connected the 9v battery to power the arduino using GND and 5+ while directly taking from GND and Power from the battery. (not using power from arduino)

All of these things have not worked and I have reached the limit of what I can think of doing differently.

I'd very much appreciate any help

Thanks in advance :grinning:
Nick

If you are using a 9V battery like this.


Don't.

The search box is a useful tool.


For instance, using the words "9v battery" in the search box one could find lots of reasons why this may be a bad idea.

Other search words include "level shifter", "round", "millis()",

Post a schematic.

Post an image of your project.

Which Micro Controller are you using?

You put 9V onto the 5V of the MCU?

Hi,
I have been using a battery like that but that is why I have also tried a 3.7v battery. I have connected 9v battery to 5v (I know it should be regulated) but no damage has been done it still works with the computer.
Thanks for the ideas
Nick

Servo stuff

Power externally.

power distribution blocks.



Post a schematic.

Post an image of your project.

Which Micro Controller are you using?

I am using an arduino Nano

Servos usually require 4.8 to 6V at 1 Ampere (small servo), up to 3 Amperes (large servo).
A 4xAA battery pack will handle 2 small servos or 1 large one.

The problem has been sovled. By connecting the GND wire from the 3.7v LiPo to both the servo and the arduino GND has solved the problem. Thanks!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.