Go Down

Topic: code for servo works in tinkercad but not when I load to Uno. (Read 606 times) previous topic - next topic

kwatts1000

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

AWOL

Quote
Am I missing something???
A common ground?
An adequate power supply?


slipstick

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


PaulS

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 art of getting good answers lies in asking good questions.

paniha

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.

outsider

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():
Code: [Select]
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.
Code: [Select]
#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);
  }
}

TomGeorge

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... :)
Everything runs on smoke, let the smoke out, it stops running....

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.

AWOL

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?

vinceherman

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?

Robin2

Are you able to draw any conclusions from this?
Yeah. Kind folk have been wasting their time :)

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

PaulS

The art of getting good answers lies in asking good questions.

vinceherman

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.

kwatts1000

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.

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up