New to coding and circuits. Please help!

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

Many thanks

Archie

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

Micro servo SG92R

Just google "arduino Micro servo SG92R" and you would get arduino tutorial:
https://create.arduino.cc/projecthub/SBR/working-with-a-micro-servo-86ec6b

There is useful stuff but not what I’m looking for

const byte ServoPin = 3;
const byte ButtonPin = 4;

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

what do you looking for? wait, i correct me. What do you want from us more?

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

how many results from internet search has you already red?
in post #1 is the functional sketch, in #4 ist how to wire it.

Really?, but your question was "Would someone be able to let me know where I put my wires." and that arduino tutorial explain well on that question?

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

1 Like

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.

more about pull up resistor:

thanks everyone for your help

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

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