Closed akshargupta84 closed 4 years ago
Hi @akshargupta84,
From the product page on Sparkfun I can see that your servo supports 160 degrees. You need to find out which specific values to use for your servo in the lines:
myservo.write(180);
and
myservo.write(0);
I created a helper sketch for you, which you can find here: https://github.com/PacktPublishing/Hands-on-Internet-of-Things-with-MQTT/blob/master/ch5/arduino/ch5_extra_01_find_servo_positions/ch5_extra_01_find_servo_positions.ino
I hope this helps.
Thanks for the quick reply Tim. When i connected the board and the motor today to my laptop to test your recommendation above, i came across a new problem. With the motor connected, arduino is not able to detect the port for the connected board. It can detect with the motor not connected to the board. Also, i noticed today that the CHRG light is very rapidly blinking on the arduino board. I might be missing something very basic but is there something i am doing wrong here?
Akshar
Okay, the port error went away after I unplugged and replugged the board.
I tried different values for the part of the code you suggested. However, the behavior remains similar. The main issue is that when I enter 1, the motor turns but the turns again in the other direction and then comes back to zero.
my expectation is that when i enter 1, it would turn by a certain angle and remain there. Is that correct? Also, for the second line
myservo.write(0)
does this run only when I enter 0 or will it run even after I enter 1?I assumed it runs only after i enter 0 which means the motor should not turn back at all when i enter 1, right?
I tried uploading the video showing how it behaves when I enter 1 video link here - https://streamable.com/roao1c
What code are you talking about in your latest comment? The one you posted initially, so this one?
#include <Servo.h>
Servo myservo;
void setup() {
Serial.begin(9600);
myservo.attach(9);
}
void loop() {
// myservo.write(0);
while (Serial.available() > 0) {
int inputValue = Serial.parseInt();
if (inputValue == 1) {
myservo.write(180);
} else {
myservo.write(0);
}
}
}
In this code there is no automatic back and forth. If your servo is doing it I suspect that could not successfully upload the code and your Arduino is still running older code (servo sweep example maybe).
After you clicked the upload icon have a look at the Arduino console (in the bottom of the app) if you see any errors.
What can also try to verify that you are able to upload successfully is uploading the blink sketch, which makes the on-board LED blink:
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
You can also find this in Arduino under File —> Examples —> 01.Basic —> Blink.
Yes, that's the code I am talking about. I changed the angles to 180 and 90 and they seem to be the right ones to use. However, the problem of the motor coming back to 0 still persists. Code below.
#include <Servo.h>
Servo myservo;
void setup() {
Serial.begin(19200);
myservo.attach(9);
}
void loop() {
// myservo.write(0);
while (Serial.available() > 0) {
int inputValue = Serial.parseInt();
if (inputValue == 1) {
myservo.write(180);
} else {
myservo.write(90);
}
}
}
I have also uploaded a video showing the whole thing. Do you think it could be because of the motor specs? https://streamable.com/ozd82w
I also tried the blink code and it worked perfectly Note: I changed baud to 19200 in the code above as well as in the monitor to see if that changes anything, it didn't.
@akshargupta84 My guess is that the servo you picked does not play well with 3.3V, which the Arduino MKR 1010 Wifi is running on.
Quote from the book:
A cheap servo motor is the Tower Pro SG90 Micro Servo. For servo motors, the specified voltage often is 4.8V (instead of 3.3V, as needed to be used with the Arduino MKR WiFi 1010), but chances are that it will work just fine using 3.3V. Please make sure you don't buy a digital servo that is not especially made for 3.3V. Digital ones made for 4.8V will most likely not work with 3.3V: https://www.adafruit.com/product/169
If you have the chance I would try the recommended servo or a similar small one.
Thank Tim. I suspected it had something to do with the motor. I will try the recommended one. I did buy this one from the shopping list" doc on this git repo. The link there takes me to https://www.sparkfun.com/products/14760
@akshargupta84 Oh, that’s embarrassing 🙈 The file _shoppinglist.md included the link to a non-tested servo motor. In chapter 5 in the book another(tested) one is being mentioned – Tower Pro SG90 Micro Servo. I corrected _shoppinglist.md.
Sorry for the inconvenience. I hope you will find another use for the servo motor.
Hi Tim, I tested with the recommended motor and was facing the same issue.
However, I have figured out the main problem. It was as simple is selecting "no line ending" in the Arduino Serial monitor. By default it is at "Newline". This means the serial monitor sends 2 values in serial.parseint(). first one is what we enter and the second one is 0. Hence, the motor kept returning back to 0. This link has more details -
Might be helpful to add this little detail in the book I will go ahead and close this issue now.
Thanks for your help
Hi @akshargupta84, Glad you figured it out. Thanks for reporting your solution. If it comes to a second edition I will make sure to include your suggestion.
Hi, I am currently on Chapter 5. I bought all the recommended parts including the Arduino MKR WiFi 1010 board. I tested the servo motor with the sweep code and everything seemed fine.
I wrote the code for normal servo angle control (ch5_01_servo_serial) but it does not behave in the manner it is supposed to as per the code. When i enter 1 in the serial monitor, the servo rotates only 90 degrees (even though i say servo.write(180) in my code, and then it rotates 180 in the other direction, waits for about 1 second and then returns to zero. When i enter 0 in the serial monitor, it rotates 90degrees and then return to zero. Code pasted below. Can you help me understand what am I doing wrong? This is the motor I bought - https://www.sparkfun.com/products/14760
include
Servo myservo;
void setup() { Serial.begin(9600); myservo.attach(9); }
void loop() { // myservo.write(0); while (Serial.available() > 0) { int inputValue = Serial.parseInt(); if (inputValue == 1) { myservo.write(180); } else { myservo.write(0);
} }