Hello all,
I encountered a very strange problem with my servo motors and I am driving myself nuts trying to solve the issue. Basically, when I uploaded a piece of working code that I previously wrote I am able to drive the servo, so I know for sure that the motor itself, the connection, nor the board are faulty. However when I upload a very simple code that only calls servo.write() method for some reason the servo doesn't move at all and doesn't even make a sound. I made sure the program is executing by serial printing. That made me believe that the only issue is in the code but I just don't understand how it could be different. My old code that works creates a class for writing into the servo but fundamentally I am calling the same method servo.write(). Here is the "simple code" I can't get to work. If I could get any insight that would be greatly appreciated! I am powering the servo using a separate 5V external source and I made sure that the servo is still working by uploading the old code. Thanks!
#include <Servo.h>
Servo myservo;
void setup()
{
Serial.begin(9600);
myservo.attach(2);
}
void loop() {
myservo.write(50);
Serial.print(myservo.read());
delay(1000);
myservo.write(100);
Serial.print(myservo.read());
delay(1000);
}
For your reference, here is the old code that uses class:
--------------Header-----------------
class gripper {
private:
byte pin;
int gripAngle;
int ungripAngle;
public:
Servo servo1;
int currentAngle;
gripper(byte pin);
void init();
void grip();
void ungrip();
void grip(int rate);
void printCurrentAngle();
};
-------------Implmentation-----------
#include "gripper.h"
gripper::gripper(byte pin) {
this->pin = pin;
ungripAngle=80;
gripAngle=10;
}
void gripper::init() {
servo1.attach(pin);
servo1.detach();
}
void gripper::grip() {
servo1.attach(pin);
servo1.write(gripAngle);
delay(500);
printCurrentAngle();
servo1.detach();
}
void gripper::ungrip() {
servo1.attach(pin);
servo1.write(ungripAngle);
delay(500);
printCurrentAngle();
servo1.detach();
}
void gripper::grip(int rate) {
int current = servo1.read();
servo1.attach(pin);
for (int i = current; i >= gripAngle; i--) {
servo1.write(i);
printCurrentAngle();
delay(rate);
}
servo1.detach();
}
void gripper::printCurrentAngle(){
Serial.print("Current Angle: ");
Serial.print(servo1.read());
Serial.print("\t");
}
--------------------------------------
@antwang64
Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.
Continued cross posting could result in a time out from the forum.
Please take a few moments to Learn and Use The Forum
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 frustrating for you and frustrating for us.
The people who try to help with your pro…
It will help you get the very best out of the forum in the future.
Your OS and version can be valuable information, please include it along with extra security you are using. Antivirus etc.
Always list the version of the IDE you are using and the board version if applicable.
Use quote or add error messages as an attachment NOT a picture.
How to insert an image into your post. ( Thanks @sterretje )
Add your sketch where applicable but please use CODE TAGS https://drive.google.com/file/d/1vF0OE4HozgyW_3oFENjem8DCrbrNoZ88/view?usp=share_link
Add a SCHEMATIC were needed even if it is hand drawn
Add working links to any specific hardware as needed (NOT links to similar items)
Remember that the people trying to help cannot see your problem so give as much information as you can
COMMON ISSUES
Ensure you have FULLY inserted the USB cables.
Check you have a COMMON GROUND where required. ( Thanks @Perry )
Where possible use USB 2.0 ports or a USB 2.0 POWERED HUB to rule out USB 3.0 issues.
Try other computers where possible.
Try other USB leads where possible.
You may not have the correct driver installed. CH340/341 or CP2102 or FT232 VCP Drivers - FTDI
There may be a problem with the board check or remove your wiring first.
Remove any items connected to pins 0 and 1.
COMPUTER RELATED
Close any other serial programs before opening the IDE.
Ensure you turn off any additional security / antivirus just to test.
There may be a problem with the PC try RESTARTING it.
You may be selecting the wrong COM port.
Avoid cloud/network based installations where possible OR ensure your Network/Cloud software is RUNNING.
Clear your browsers CACHE.
Close the IDE before using any other serial programs.
Preferably install IDE’s as ADMINISTRATOR or your OS equivalent
ARDUINO SPECIFIC BOARDS
CH340/341 based clones do not report useful information to the “get board info” button.
NANO (Old Types) some require you to use the OLD BOOTLOADER option.
NANO (ALL Types ) See the specific sections lower in the forum.
NANO (NEW Types ) Install your board CORE’s.
Unless using EXTERNAL PROGRAMMERS please leave the IDE selection at default “AVRISP mkII”.
Boards using a MICRO usb connector need a cable that is both DATA and CHARGE. Many are CHARGE ONLY.
CREATE editor install locations.
On macOs ~/Applications/ArduinoCreateAgent-1.1/ArduinoCreateAgent.app/Contents/MacOS/config.ini
On Linux ~/ArduinoCreateAgent-1.1/config.ini
On Windows C:\Users[your user]\AppData\Roaming\ArduinoCreateAgent-1.1
Performing the above actions may help resolve your problem without further help.
Language problem ?
Try a language closer to your native language:
Thanks to all those who helped and added to this list.
JCA34F
March 21, 2021, 10:37pm
3
What do the Serial.prints say? What are the servo + wire, - wire and signal wire connected to?
The main difference I can see is that the old code is doing multiple writes moving only 1 degree at a time. The new code is trying to move in big jumps which takes more power. That can causes problem if the servo power is marginal. What servo do you have and what exactly is the "separate 5V external source"?
Steve
The external power source is a wall plug 5V power supply. Turns out that I need to ground the Arduino board to the same GND as the motors, at least that did the trick for me!
It is weird however that when I didn't ground the board my old code that uses class still worked....
Thanks for taking the time to reply!
system
Closed
July 19, 2021, 11:19pm
6
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.