Circuit and code works in simulation but not doing anything when done it real life

code here (made using blocks in tinker cad):

// C++ code
//
#include <Servo.h>

Servo servo_9;

Servo servo_10;

Servo servo_8;

void setup()
{
pinMode(5, INPUT);
servo_9.attach(9, 500, 2500);

pinMode(3, INPUT);
servo_10.attach(10, 500, 2500);

pinMode(4, INPUT);
servo_8.attach(8, 500, 2500);

}

void loop()
{
if (digitalRead(5) < 1) {
delay(500); // Wait for 500 millisecond(s)
servo_9.write(60);
delay(500); // Wait for 500 millisecond(s)
} else {
servo_9.write(0);
}

if (digitalRead(3) < 1) {
delay(500); // Wait for 500 millisecond(s)
servo_10.write(60);
delay(500); // Wait for 500 millisecond(s)
} else {
servo_10.write(0);
}

if (digitalRead(4) < 1) {
delay(500); // Wait for 500 millisecond(s)
servo_8.write(60);
delay(500); // Wait for 500 millisecond(s)
} else {
servo_8.write(0);
}
}

this is my first try at code and using Arduino for a school project and I'm a bit stuck. I feel like I may be missing something but I don't know what, any help will be appreciate. Thanks

Looks like the power for the servos is being drawn through the board.

They will need their own power supply.
Leave the ground from the Uno to the breadboard, but remove the power.
You need an external power supply that provides at least a couple of amps at 5V.
You need to feed that power supply into the breadboard rail to power the servos, the ground wire you left connected is the signal ground.

Don't turn it on yet.

Next, your switches are backwards and will feed power from your new power supply into your board and that's no good. Even though they work in the sim it's not a good idea in meatspace.
Instead of connecting your switches to the hot, you can connect them to the ground and you can eliminate the resistors.
Instead of setting them to INPUT, you need to set the pin mode to INPUT_PULLUP; that uses an internal resistor to make the pin read high unless you connect it to ground by activating the switch.
This means if the switch reads high then it is off, but if it reads low, then it is on.

if (digitalRead(5) == LOW) {
    // switch is on
}
// or
if (digitalRead(5) == HIGH) {
    // switch is off
}

There may be more issues, but that's a start.

Hi, @ryan_haines
To add code please click this link;

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Please don't try to run servo power through a breadboard. They are only intended for low current. Servos and other motors are NOT low current devices.

Steve

@ryan_haines, your topic was moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project.

how do you recommend me power the servos?

thank you, I am very new to code etc... but I think I have fixed those problems you mentioned but aren't totally sure, the loop is again working in the simulator but just want to be sure

here is the updated code and loop

// C++ code
//
#include <Servo.h>

Servo servo_9;

Servo servo_10;

Servo servo_8;

void setup()
{
if (digitalRead(5) == LOW) {
    // switch is on
}
// or
if (digitalRead(5) == HIGH) {
    // switch is off
}
  pinMode(5, INPUT_PULLUP);
  servo_9.attach(9, 500, 2500);

  pinMode(3, INPUT_PULLUP);
  servo_10.attach(10, 500, 2500);

  pinMode(4, INPUT_PULLUP);
  servo_8.attach(8, 500, 2500);

}

void loop()
{
  if (digitalRead(5) < 1) {
    delay(500); // Wait for 500 millisecond(s)
    servo_9.write(60);
    delay(500); // Wait for 500 millisecond(s)
  } else {
    servo_9.write(0);
  }

  if (digitalRead(3) < 1) {
    delay(500); // Wait for 500 millisecond(s)
    servo_10.write(60);
    delay(500); // Wait for 500 millisecond(s)
  } else {
    servo_10.write(0);
  }

  if (digitalRead(4) < 1) {
    delay(500); // Wait for 500 millisecond(s)
    servo_8.write(60);
    delay(500); // Wait for 500 millisecond(s)
  } else {
    servo_8.write(0);
  }
}

What's the yellow thin in the upper right corner?
What's with the resistor on the power supply?
Which power supply do you have?
Please post a picture of your setup.

The yellow thing is just a multimeter I just forgot to remove. I put the resistor there so I only put 5v into the breadboard because I am using a 9v battery but I cannot get a picture right now.

Resistors reduce current (amps) not voltage.
You may have some damage, but since it's a 9V battery you may be lucky. 9V batteries do not work very well for most any Arduino project and should not be used.

You need a 5V 2A power adapter as I described in reply 2.

Also, as @slipstick correctly pointed out my mistake, please connect the power for the servos directly to the supply, as the breadboard should not have that much power through it. You should use a switch here too and that switch should be able to handle 2A as well.

It is not advisable that you proceed without the correct parts. Please feel free to post links to those parts here for review, before you order them.

Also I need a photograph of your breadboard and setup please, not just the drawing.

Keep with it and we'll get you through it :slight_smile:

I haven't changed anything because I cant at the moment so I don't think it is damaged luckily but with the 5v 2a power adapter does it go into the Arduino and then power the servos from there or another way?

thank you for all your help so far by the way

Are you in the USA? I ask because I will help you pick some parts and I need to know where to shop.

I am from Australia

@PerryBebbington Can you please help with a good supplier in Australia?

Read the reference of digitalRead(): https://www.arduino.cc/reference/en/language/functions/digital-io/digitalread/
It returns HIGH or LOW, so you can compare it with HIGH or LOW.

if (digitalRead(5) == LOW)     // okay, testing if that pin is LOW
if (digitalRead(3) < 1)        // not okay

I use a search for the reference of a function (Google, Bing, or any other).
For example, searching for "arduino servo" gives me the Servo library reference: https://www.arduino.cc/reference/en/libraries/servo/

I've always thought that Perry is from the UK; no idea why. Maybe @TomGeorge.

https://www.jaycar.com.au/

https://www.altronics.com.au/

Tom... :smiley: :+1: :coffee: :australia:

1 Like

As @sterretje pointed out, I am in the UK, I have no idea who the suppliers are in Australia.

My mistake.

1 Like

3-12V DC 12W Power Supply 7DC Plugs and USB Outlet | Jaycar Electronics

The power supply is expensive but you get what you pay for. The cheap ones aren't worth the risk.

This is probably your cheapest and easiest option. After you revise your code, you can upload it, disconnect the USB, then power the board with the power supply and the project should work just fine.

Alternatively you can use a servo shield for your Uno.