How do I connect things to a button?

I’ve searched for a long time on how to wire a four pin button, and now that I think I have it, I simply can’t connect it to my servo motor. This is driving me crazy! I will send A picture of my wiring and code, so if anyone knows what the problem is please tell me.



(Black/orange wires lead to pins, blue wires lead to gnd and red wires lead to 5v )

Welcome to the forum

Please do not post pictures of your code

In the IDE, right click and select "Copy for forum" then paste the clipboard contents into a new reply to this topic. The copy from the IDE will have added code tags to the start and end of the code to make it easier to read and copy for examination

Your picture of the wiring is also not very clear. It would be better if you drew a schematic of your project with pencil and paper and posted a picture of that

Can I suggest that you use red wires for 5V connections, black wires for GND connections and other colours for connections to pins and switches. The electrons won't care but those colours are more conventional

As to your circuit, what is keeping the button pin LOW when the button is not pressed ?

It is not a good idea to power a servo from the Arduino 5V pin as it almost certainly cannot supply enough current to drive the servo under all conditions and you may damage the Arduino. Consider using an external power supply for the servo

buttons are typically connected between the pin and ground, the pin configured as INPUT_PULLUP to use the internal pullup resistor which pulls the pin HIGH and when pressed, the button pulls the pin LOW.

consider

#include <Servo.h>

Servo servo1;
const byte PinBut = 2;

// -----------------------------------------------------------------------------
void loop (void)
{
    if (LOW == digitalRead (PinBut))    // pressed
        servo1.write (0);
    else
        servo1.write (180);
}

// -----------------------------------------------------------------------------
void setup (void)
{
    Serial.begin (9600);

    pinMode (PinBut, INPUT_PULLUP);
    servo1.attach (13);
}

Problem, bread board power rail split:


Solution, bread board power rail split:


And that blue wire resting in the hole of the breadboard is not plugged in.

Do you have an LED and a resistor in the range of 330 Ohms to 2000 Ohms?

It is very important to use a current-limiting resistor for LEDs

You could wire the resistor and the LED in serial.

GND-----resistor------cathode_of_LED anode_of_LED----------+5V

With this you have a very simpe test-device for testing if somewhere electricity is conducted or not

GND-----resistor------cathode_of_LED anode_of_LED---contact _ 1_of_switch....... contact _ 2_of_switch -------+5V

for testing if the switch is opening / closing of the button
These buttons look this way internally
image
This means of you connect to the wrong pins there is always permanent contact

best regards Stefan

1 Like

I descovered something in your picture:

You have connected the button to arduino IO-pin 7
but your code is reading in IO-pin 2

const int buttonPin = 2;

pinMode(buttonPin, INPUT);
buttonState = digitalRead(buttonPin);

you have to plug in the jumperwire into Arduino-IO-pin 2

best regards Stefan

1 Like

In addition to what @StefanL38 pointed out, check your connections:

Hi @paulamotta ,

watch the simulation until
CONTINUE microcontroller goes on appears

I have build two WOKWI-simulations that rebuild your exercise
one to one like you did using the blocking command delay()

delay() means:
stop microcontroller comletely until delay-time is over.

and then a second version that uses non-blocking timing.

This second version does blink in LED all the time

Even in case the program keeps the servo in position 180 and is waiting for
turning the servo back to position 0

As soon as you want your program to be responsive to button-presses or if you want to do
a second, third, fourth action in parallel to some other action non-blocking timing is required

It will take some time to understand this second code-version but it shows a somehow advanced functionality as it blinks the LED continiously in parallel to the waiting

additionally here is a demo where the LED-blinking and the servo have timing in a blocking way
through the use of delay()
The wait-times are 2 seconds and 3 seconds long to make it easier to realise what is happening.

You have to keep the button pressed down until the LED goes of
in all other cases the program does not detect the button-press

best regards Stefan

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