I’ve posted another topic before this asking this:
I Have a simple push-button with 4 pins and a simple micro servo. I'm using an Arduino nano. I don't have any resistors and don't know if I will need them. What I'm trying to do is this:
When I start the process, the servo starts at 0 degrees
When I press down the button (hold) the servo moves from 0 to 180 degrees, not more
When I let go of the button, the servo moves from 180 degrees back to 0 degrees
I'd love for someone to help me with regards to the code, and the pins to attach my wires to on the arduino.
I received this code which I believe is the solution, but didn’t receive where to put my wires on the arduino. (I’m a beginner btw) :
#include <Servo.h>
const byte ServoPin = 3;
const byte ButtonPin = 4;
Servo servo;
// * When I start the process,
void setup()
{
// the servo starts at 0 degrees
servo.write(0);
servo.attach(ServoPin);
pinMode(ButtonPin, INPUT_PULLUP);
// NOTE: The internal pull-up resistor will keep the
// pin reading HIGH when not connected. The pin
// will read LOW when connected to Ground through
// a closed switch.
}
void loop()
{
// * When I press down the button (hold)
if (digitalRead(ButtonPin) == LOW)
{
// the servo moves from 0 to 180 degrees, not more
servo.write(180);
}
else // * When I let go of the button,
{
// the servo moves from 180 degrees back to 0 degrees
servo.write(0);
}
}
Would someone be able to let me know where I put my wires. I HAVE AN ARDUINO NANO
Please, could you tell what type of servo you have?
for example if its servo that works in 5V and can handle PWM (1kHz), there a lot tutorials on google, but you need to know servo type.
for example, look google "arduino micro servo connection" and you will get some ideas how to connect. but you need to know servos specs first, not all of them are ok. to connect directly Arduino
These lines tell you which pins to attach the button and servo.
The button should be attached between the Arduino pin and ground. No resistor needed. Use a diagonal pair of pins on the button, leave the other pins unconnected.
The servo should be connected to 5V, ground and the Arduino pin. No resistors required. Only one servo can be connected like this. 2 servos would be too much for the Arduino and a separate power supply would be needed. Ideally an separate power supply should be used even for one servo.
great thank you. Are you able to make a circuit diagram if possible as im still a bit confused as to where i put my button. Thank you!!
sorry for being a pain
im looking for a tutorial that explains how i wire and code a servo to move to 180 degrees when i press down a button, and when i stop pressing it down, it moves back to 0
That didn’t include the button. I understand the basics of wiring a servo. But when the button is introduced I don’t know what to do and which wire from the servo I use
you connect servo as an standalone item and button as its own, they doesnt have any dependencies.
for connecting button:
you dont need resistor to connect it to GND, but if that doesnt work, you can connect button between gnd & IO pin, but then you need to use buildin pullup resistor.