Trying to create a Mini Barrier Gate w/Arduino and Servo

Hello All,

I am in need of some help. I am a noob with coding and arduino but need to create a mini barrier gate. when 5v output from IP cam to Analog Input on the board, i need the servo motor to move from 0° to 90­° then come back down to 0°. I have the servo connected to the boards 5v power, The IP camera can produce 5v out when triggered. I have created some code and I have the servo moving, it is just not moving they way I need it to. Please help!!!

#include <Servo.h>
Servo myservo;

int camPin = A5;
int servPin = 3;
int pos = 0;


void setup()
{
  // put your setup code here, to run once:
  pinMode(servPin, OUTPUT);
  myservo.attach(9);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (analogRead(A5) > 512)
  {
    digitalWrite(3, HIGH);
    myservo.write(90);
    delay(1000);
    digitalWrite(3, LOW);
    myservo.write(0);
  }
  else
    digitalWrite(3, LOW);
  myservo.write(0);

}
1 Like

And how is it moving now?

What is the servo doing?

Post images of your project.

How is the project powered? How is the servo being powered?

No Arduino board is a power supply. Most servo problems experienced by new users can be traced to lack of power. Servos draw a lot of current and so need their own power supply. A 4 AA cell battery pack is a popular servo power supply.

Very bad idea. The Arduino is not a power supply, and cannot be used to power motors and servos. Ignore the many terrible tutorials that recommend doing so.

As previously suggested, use a 4XAA battery pack to power the servo, and connect the grounds.

Thank you for your response. I have a feeling that the power was the issue but everything I read online and the video’s I watch had the servo connected to the board. I will grab a AA pack and test again. What do you think of the code do you think I’m on the right track?

Thanks for the response. It’s moving Sporadically so I figured it was the power but everything I seen online said to connect the servo to the board

Thanks for the response. I figured it was the power as the servo would just move forward backwards and just kept looping and not what I had put in the code. Everything I saw online showed connecting to the board and that is what I did it. I will grab a 4AA pack tomorrow and rest it out. What do you think of my code?

I see nothing wrong with the code.

Don't believe all that you see on the internet.

What do you need ?

I see nothing wrong with the code.

I have the servo moving, it is just not moving they way I need it to.

You still haven't told us what the servo does.

Than you all for your input. I went out and got a 4AA BATTERY holder but now my servo does not respond. I have used a multimeter to test the output of the batteries and they have voltage coming out but the servo doesn’t move. What am I doing wrong. Below is a picture of my set up

You must connect the battery pack ground to the Arduino ground. So battery pack, servo and Arduino grounds all connected together.

Please post a schematic.

Yea, what was wrote in post#13.

Connect the battery ground to the Arduino ground.

Thank you all for your input i have managed to get the servo powered. I am able to send it to different positions using myservo.write(0) or (90) or (180) but i am not able to utilize the analog input to trigger the output. I am using an IP camera that provides 5v output and connected the + to A5 and (-) for the IP camera to ground on the board. i am using the code below but when i turn on the 5v from the camera nothing happens. What i would like to happen is when the 5v hits the A5 input, the servo goes to position 90° then delay for 1sec then come back to 0° and wait until the next trigger of 5v to A5:

#include <Servo.h>
Servo myservo;

int camPin = A5;
int servPin = 6;
int pos = 0;


void setup()
{
  // put your setup code here, to run once:
  pinMode(servPin, OUTPUT);
  myservo.attach(6);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (analogRead(A5) > 512)
  {
    digitalWrite(6, HIGH);
    myservo.write(90);
    delay(1000);
    digitalWrite(6, LOW);
    myservo.write(0);
  }
  else
    digitalWrite(6, LOW);
  myservo.write(0);

}

Write a separate little program to test the analog input and verify proper operation. Did you forget to connect the signaling device ground to the Arduino ground?

Is the camera ground connected to the MCU's ground?

Thanks. No not this time I have connected the cameras (-) to ground on the board. That I have always done

No i have the camer connected to ground.

Put some serial prints in there to see what voltage you get from the camera.