code for servo works in tinkercad but not when I load to Uno.

I created a code on tinkercad that allows me to press a button and a servo will turn 35 degrees and after 2 sec it will rotate back to 0. My problem is that when I load it to the Arduino it doesn’t do what it should. the button is working because i tested it with an LED and the servo is getting power from the correct pins because it is shaking. Am I missing something???

#include <Servo.h>

int position = 0;

Servo servo_3;

void setup()
{
pinMode(7, INPUT);
servo_3.attach(3);

}

void loop()
{
// race starts
if (digitalRead(7) == HIGH) {
// when start button is pressed, gate will drop
servo_3.write(35);
delay(1500); // Wait for 1500 millisecond(s)
servo_3.write(0);
} else {
// when no buttons are pressed,
// then the gate will remain closed
servo_3.write(0);
}
}

servo11.ino (439 Bytes)

Am I missing something???

A common ground?
An adequate power supply?

O.k. so it "doesn't do what it should". What does it do, in detail?

Have you tried running the Sweep example program from the IDE to prove that the servo will do more than just shake?

What sort of servo is it and how is it powered. If it's from the Arduino 5V pin it had better be a really tiny servo or it's likely not to work and quite possibly will damage the Arduino.

Steve

The servo in tinkercad requires no current. The one in real life does.

If "the correct pins" means 3 Arduino pins, you have the servo wired wrong.

The code itself looks fine to me;

The servo can be shaking yet not have enough power to run.

Try loading the Servo-> Sweep example and use that to see if your servo is actually working.

If the Sweep sketch work, check if it's stalling for a while on each extreme, your servo may not have a full 180 degree range of motion, some are designed for 90 degrees, in which could equate to a range of 45-135 degrees depending on make and quality of servo.

If it doesn't work with that sketch, you may have to check your wiring, and if the servo is under load (actually moving something) the arduino power rail may not be delivering enough current, so try running it unloaded if that is the case.

Oh, and if you're running 3.3v, considering going 5V, i believe most servos are made for 6V, but will run okay at 5.

Do you have a 10k pulldown resistor from pin 7 to GND?
Best way is one button pin to GND, other button pin to pin 7, in setup():

pinMode(7,INPUT_PULLUP);

Pin 7 will be LOW when pressed and HIGH when not, you will need to invert pin states in your program.

#include <Servo.h>

int position = 0;

Servo servo_3;

void setup()
{
  pinMode(7, INPUT_PULLUP);
  servo_3.write(0);
  servo_3.attach(3);
}
void loop()
{
  // race starts
  if (digitalRead(7) == LOW) {
    // when start button is pressed, gate will drop
    delay(1500); // Wait for 1500 millisecond(s)
    servo_3.write(0);
  } else {
    // when no buttons are pressed,
    // then the gate will remain closed
    servo_3.write(0);
  }
}

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html . Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

to add more info to my situation. I am trying to use a 20kg servo. When I plug in a 9kg servo everything works as it should. The 20kg servo have a working voltage range of 4.8v-6.8v. Again, everything works with a micro servo.

Have you considered that a servo capable of moving 20kg will draw more current than one capable of moving 9kg, will draw more current than a micro servo, irrespective of voltage?

In reply #1, AWOL suggested that you might not have an adequate power supply.
In reply #2, slipstick warned against powering the servo from the 5v pin, admitting that it might work for a tiny servo.
In reply #3 PaulS suggests that servos require current.
In reply #4, paniha suggests that a shaking servo might not have enough power to run.

Are you able to draw any conclusions from this?

vinceherman:
Are you able to draw any conclusions from this?

Yeah. Kind folk have been wasting their time :slight_smile:

...R

Robin2:
Yeah. Kind folk have been wasting their time :slight_smile:

...R

+1 for making me laugh.

So, while we are poking fun a bit on this, we are also interested in your issue and want to help.

We suspect that you are not using a proper power supply for your large servo.
Google "arduino servo separate power supply" and you will find many examples of how to handle this.

If you think there might be another issue, please do follow the request above (TomGeorge?) and post a picture of your wiring diagram. Pencil, paper and a camera are good enough.

Another update:
I ran a sweep and the servo works as it should without any additional power. I also tried my code with the additional power with still no luck. the code still works with the 9kg servo but not the 20. Lastly, I'm new to all of this so don't get upset if I need anyone to clarify something. I figured I wouldn't have any trouble make a servo turn on command but i guess so.

Make a simple pencil drawing showing how you have everything connected and post a photo of the drawing. See this Simple Image Upload Guide

The strong consensus is that your problem is either due to a wiring problem or an inadequate power supply.

...R

kwatts1000:
to add more info to my situation. I am trying to use a 20kg servo. When I plug in a 9kg servo everything works as it should. The 20kg servo have a working voltage range of 4.8v-6.8v. Again, everything works with a micro servo.

Can you please provide links to the two servo that you are using ?

I ran a sweep and the servo works

Which servo and how was it powered ?

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you please post some pictures of your project so we can see your component layout, including the power supply for the servo?

Thanks.. Tom... :slight_smile:

So I found a solution. I looks like I can’t just use the servo.write() function. that only works with small servos. I had to use a math function and use the PWM and step the servo down. I changed the code to this and it now works:
#include <Servo.h>

int pos = 0;

Servo servo_3;

void setup()
{
pinMode(2, INPUT);
servo_3.attach(3);

}

void loop()
{
// Start Race
if (digitalRead(2) == 1) {
for (pos = 0; pos <= 35; pos += 1) {
// tell servo to go to position in variable ‘pos’
servo_3.write(pos);
// wait 10 ms for servo to reach the position
delay(10); // Wait for 10 millisecond(s)
}
delay(1500); // Wait for 1500 millisecond(s)
for (pos = 35; pos >= 0; pos -= 1) {
// tell servo to go to position in variable ‘pos’
servo_3.write(pos);
// wait 10 ms for servo to reach the position
delay(10); // Wait for 10 millisecond(s)
}

thank you all for your help. If you have any other input in will be glad to listen.
} else {
servo_3.write(0);
}
}

I looks like I can't just use the servo.write() function. that only works with small servos. I had to use a math function and use the PWM and step the servo down.

You need to expand on that.

It still sounds like a power supply problem to me.

Consider: in my attic is my kids slot-car gear. The cars have 12 V motors.
Outside my front door is a two litre diesel car.
It has a 12 V starter motor.
See the issue?

Please remember to use code tags when posting code

Hi,
It looks like it is still a power supply problem.

In your original code you are signalling the servo to go from 0 straight to 35deg as fast as possible.

In you final code you are signalling the servo to go from 0 to 35deg in small steps, that is slower than in the previous code.

In moving in 1deg steps you need less current to drive the servo motor as you are not asking for the fastest response from 0 to 35deg.

Tom..... :slight_smile:
PS. Can you please post a link to specs/data or where you purchased the servos please?