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

**URL:** <https://forum.arduino.cc/t/circuit-and-code-works-in-simulation-but-not-doing-anything-when-done-it-real-life/921971>\
**Category:** Motors, Mechanics, Power and CNC\
**Created:** [November 5, 2021, 2:08am UTC](https://forum.arduino.cc/t/circuit-and-code-works-in-simulation-but-not-doing-anything-when-done-it-real-life/921971 "2021-11-05T02:08:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryan\_haines](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ryan_haines/32/696445_2.png) [@ryan\_haines](https://forum.arduino.cc/u/ryan_haines)\
**Post date:** [November 5, 2021, 2:08am UTC](https://forum.arduino.cc/t/circuit-and-code-works-in-simulation-but-not-doing-anything-when-done-it-real-life/921971/1 "2021-11-05T02:08:34Z")

</div>

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);  
}  
}

 ![Shiny Fyyran-Borwo](https://europe1.discourse-cdn.com/arduino/original/4X/2/d/d/2dd489417021042d5a75b0e1362e3f7f52ef9199.png)

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

---

<div class="post-metadata">

**Author:** ![er\_name\_not\_found](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/er_name_not_found/32/647693_2.png) [@er\_name\_not\_found](https://forum.arduino.cc/u/er_name_not_found)\
**Post date:** [November 5, 2021, 2:23am UTC](https://forum.arduino.cc/t/circuit-and-code-works-in-simulation-but-not-doing-anything-when-done-it-real-life/921971/2 "2021-11-05T02:23:32Z")

</div>

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.

```cpp
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.

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [November 5, 2021, 7:02am UTC](https://forum.arduino.cc/t/circuit-and-code-works-in-simulation-but-not-doing-anything-when-done-it-real-life/921971/3 "2021-11-05T07:02:44Z")

</div>

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

> [@How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966#posting-code):
>
> See also [FAQ - Arduino Forum](https://forum.arduino.cc/faq) for general rules on forum behaviour and etiquette. Hello, Welcome to the Arduino Forum. This guide explains how to get the best out of this forum. Please read and follow the instructions below. Being new here you might think this is having rules for the sake of rules, but that is not the case. If you don’t follow the guidelines all that happens is there is a long exchange of posts while we try to get you to tell us what we need in order to help you, which is fru…

Thanks.. Tom... 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![slipstick](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@slipstick](https://forum.arduino.cc/u/slipstick)\
**Post date:** [November 5, 2021, 8:33am UTC](https://forum.arduino.cc/t/circuit-and-code-works-in-simulation-but-not-doing-anything-when-done-it-real-life/921971/4 "2021-11-05T08:33:45Z")

</div>

> [@er\_name\_not\_found](#):
>
> 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.

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

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [November 5, 2021, 4:39pm UTC](https://forum.arduino.cc/t/circuit-and-code-works-in-simulation-but-not-doing-anything-when-done-it-real-life/921971/5 "2021-11-05T16:39:52Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![ryan\_haines](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ryan_haines/32/696445_2.png) [@ryan\_haines](https://forum.arduino.cc/u/ryan_haines)\
**Post date:** [November 8, 2021, 1:22am UTC](https://forum.arduino.cc/t/circuit-and-code-works-in-simulation-but-not-doing-anything-when-done-it-real-life/921971/6 "2021-11-08T01:22:55Z")

</div>

how do you recommend me power the servos?

---

<div class="post-metadata">

**Author:** ![ryan\_haines](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ryan_haines/32/696445_2.png) [@ryan\_haines](https://forum.arduino.cc/u/ryan_haines)\
**Post date:** [November 8, 2021, 1:27am UTC](https://forum.arduino.cc/t/circuit-and-code-works-in-simulation-but-not-doing-anything-when-done-it-real-life/921971/7 "2021-11-08T01:27:34Z")

</div>

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

```cpp
// 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);
  }
}
```

 ![Sizzling Bigery-Vihelmo](https://europe1.discourse-cdn.com/arduino/original/4X/d/b/5/db5e081ab00bef986a16993ba0d7e7d4d52cc2e5.png)

---

<div class="post-metadata">

**Author:** ![er\_name\_not\_found](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/er_name_not_found/32/647693_2.png) [@er\_name\_not\_found](https://forum.arduino.cc/u/er_name_not_found)\
**Post date:** [November 9, 2021, 1:19pm UTC](https://forum.arduino.cc/t/circuit-and-code-works-in-simulation-but-not-doing-anything-when-done-it-real-life/921971/8 "2021-11-09T13:19:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ryan\_haines](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ryan_haines/32/696445_2.png) [@ryan\_haines](https://forum.arduino.cc/u/ryan_haines)\
**Post date:** [November 9, 2021, 10:49pm UTC](https://forum.arduino.cc/t/circuit-and-code-works-in-simulation-but-not-doing-anything-when-done-it-real-life/921971/9 "2021-11-09T22:49:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![er\_name\_not\_found](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/er_name_not_found/32/647693_2.png) [@er\_name\_not\_found](https://forum.arduino.cc/u/er_name_not_found)\
**Post date:** [November 9, 2021, 11:03pm UTC](https://forum.arduino.cc/t/circuit-and-code-works-in-simulation-but-not-doing-anything-when-done-it-real-life/921971/10 "2021-11-09T23:03:18Z")

</div>

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](https://forum.arduino.cc/t/circuit-and-code-works-in-simulation-but-not-doing-anything-when-done-it-real-life/921971/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 🙂

---

<div class="post-metadata">

**Author:** ![ryan\_haines](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ryan_haines/32/696445_2.png) [@ryan\_haines](https://forum.arduino.cc/u/ryan_haines)\
**Post date:** [November 10, 2021, 12:08am UTC](https://forum.arduino.cc/t/circuit-and-code-works-in-simulation-but-not-doing-anything-when-done-it-real-life/921971/11 "2021-11-10T00:08:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![er\_name\_not\_found](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/er_name_not_found/32/647693_2.png) [@er\_name\_not\_found](https://forum.arduino.cc/u/er_name_not_found)\
**Post date:** [November 10, 2021, 12:10am UTC](https://forum.arduino.cc/t/circuit-and-code-works-in-simulation-but-not-doing-anything-when-done-it-real-life/921971/12 "2021-11-10T00:10:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ryan\_haines](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ryan_haines/32/696445_2.png) [@ryan\_haines](https://forum.arduino.cc/u/ryan_haines)\
**Post date:** [November 10, 2021, 12:23am UTC](https://forum.arduino.cc/t/circuit-and-code-works-in-simulation-but-not-doing-anything-when-done-it-real-life/921971/13 "2021-11-10T00:23:02Z")

</div>

I am from Australia

---

<div class="post-metadata">

**Author:** ![er\_name\_not\_found](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/er_name_not_found/32/647693_2.png) [@er\_name\_not\_found](https://forum.arduino.cc/u/er_name_not_found)\
**Post date:** [November 10, 2021, 12:25am UTC](https://forum.arduino.cc/t/circuit-and-code-works-in-simulation-but-not-doing-anything-when-done-it-real-life/921971/14 "2021-11-10T00:25:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [November 10, 2021, 12:35am UTC](https://forum.arduino.cc/t/circuit-and-code-works-in-simulation-but-not-doing-anything-when-done-it-real-life/921971/15 "2021-11-10T00:35:47Z")

</div>

Read the reference of digitalRead(): [https://www.arduino.cc/reference/en/language/functions/digital-io/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.

```cpp
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/](https://www.arduino.cc/reference/en/libraries/servo/)

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [November 10, 2021, 2:15am UTC](https://forum.arduino.cc/t/circuit-and-code-works-in-simulation-but-not-doing-anything-when-done-it-real-life/921971/16 "2021-11-10T02:15:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TomGeorge](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/tomgeorge/32/215261_2.png) [@TomGeorge](https://forum.arduino.cc/u/TomGeorge)\
**Post date:** [November 10, 2021, 3:27am UTC](https://forum.arduino.cc/t/circuit-and-code-works-in-simulation-but-not-doing-anything-when-done-it-real-life/921971/17 "2021-11-10T03:27:26Z")

</div>

> [@er\_name\_not\_found](#):
>
> @PerryBebbington Can you please help with a good supplier in Australia?

[https://www.jaycar.com.au/](https://www.jaycar.com.au/)

> **[Altronics | Electronic components, AV, CCTV, Power & More](https://www.altronics.com.au/)**
>
> Suppliers of components, semiconductors, audio & computer leads, connectors, speakers, amplifiers, cctv, power supplies, test equipment, soldering gear, batteries, chargers, inverters, solar panels, tools, led lighting, racks, iot and kits.

> **[Wiltronics](https://www.wiltronics.com.au/)**
>
> Science and Technology specialists, Wiltronics has been involved in the manufacture and distribution of a large range of high quality products for over 42 years

Tom... 😃 👍 ☕ 🇦🇺

---

<div class="post-metadata">

**Author:** ![PerryBebbington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/perrybebbington/32/238766_2.png) [@PerryBebbington](https://forum.arduino.cc/u/PerryBebbington)\
**Post date:** [November 10, 2021, 8:30am UTC](https://forum.arduino.cc/t/circuit-and-code-works-in-simulation-but-not-doing-anything-when-done-it-real-life/921971/18 "2021-11-10T08:30:35Z")

</div>

> [@er\_name\_not\_found](#):
>
> @PerryBebbington Can you please help with a good supplier in Australia?

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

---

<div class="post-metadata">

**Author:** ![er\_name\_not\_found](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/er_name_not_found/32/647693_2.png) [@er\_name\_not\_found](https://forum.arduino.cc/u/er_name_not_found)\
**Post date:** [November 10, 2021, 11:41am UTC](https://forum.arduino.cc/t/circuit-and-code-works-in-simulation-but-not-doing-anything-when-done-it-real-life/921971/19 "2021-11-10T11:41:40Z")

</div>

My mistake.

---

<div class="post-metadata">

**Author:** ![er\_name\_not\_found](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/er_name_not_found/32/647693_2.png) [@er\_name\_not\_found](https://forum.arduino.cc/u/er_name_not_found)\
**Post date:** [November 10, 2021, 3:14pm UTC](https://forum.arduino.cc/t/circuit-and-code-works-in-simulation-but-not-doing-anything-when-done-it-real-life/921971/20 "2021-11-10T15:14:23Z")

</div>

[3-12V DC 12W Power Supply 7DC Plugs and USB Outlet | Jaycar Electronics](https://www.jaycar.com.au/3-12v-dc-12w-power-supply-7dc-plugs-and-usb-outlet/p/MP3312)

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

> **[Amazon.com: KEYESTUDIO Plus Board for Arduino UNO R3 with Type-C USB Cable, 3.3V...](https://www.amazon.com/KEYESTUDIO-Arduino-ATmega328P-Controller-Development/dp/B085RHTBJT)**
>
> Amazon.com: KEYESTUDIO Plus Board for Arduino UNO R3 with Type-C USB Cable, 3.3V 5V 1.5A Output Current, More Powerful Controller Board : Electronics

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.

[Next page](https://forum.arduino.cc/t/circuit-and-code-works-in-simulation-but-not-doing-anything-when-done-it-real-life/921971.md?page=2)
