Hi, I'm trying to scan my room using a servo and an ultrasonic sensor. I'm rotating the servo with an interval of 50 ms and mesuring the distance using the sensor. But I noticed that the servo might be interfering the sensor. Maybe because current? Why is this happening? Should I use an external 5v to power the servo? Moreover, my servo keeps buzzing when it's idle, it can also be resolved using an external 5v?
Thanks
PS: I tried mesuring the distance manually and using a library.
#include<Servo.h>
#include<Ultrasonic.h>
Servo servo;
int trig = 2;
int eco = 3;
int buzzer = 6;
int controlado = false;
float controle[130];
Ultrasonic ultrasonic(trig, eco);
void setup() {
servo.attach(10);
Serial.begin(9600);
Serial.println("COMEÇANDO O PROGRAMA");
}
void loop() {
if(!controlado){
Serial.println("Fazendo o escaneamento");
for(int i = 0; i < 130; i++){
servo.write(i);
delay(50);
controle[i] = distancia();
Serial.println(controle[i]);
}
controlado = true;
Serial.println("Escanemaneto finalizado.");
}
}
float distancia(){
long tempo = ultrasonic.timing();
float distancia = ultrasonic.convert(tempo, Ultrasonic::CM);
return distancia;
}
Should I use an external 5v to power the servo? Moreover, my servo keeps buzzing when it's idle, it can also be resolved using an external 5v?
Yes, and Yes!! You will damage the Arduino if you try to power motors or servos from the 5V output.
A 4xAA battery pack works for 1-2 servos. Don't forget to connect the grounds.
Finally, where did you get the library for the ultrasonic sensor? It is possible that there is a conflict between it and the servo library (both might use Timer1, for example).
Completely agree regarding the power supply.
I switched to driving my servos by I2C using a pca9685 board because I inevitably found that the Arduino produced glitches in servos that should be stationary. Some servos are more sensitive to it than others. It definitely gets worse with some Arduino libraries. That might be fine for some applications, but in others, like steadying a camera, it isn't. Nor is it anything you notice if you are moving the servos.
The ultrasonic sensors seem to be very sensitive to power supply issues. I find that even with a good power supply, hooking up a sharp IR sensor increases the rate of errors in reading ultrasonic values. Very frustrating.
jremington:
Yes, and Yes!! You will damage the Arduino if you try to power motors or servos from the 5V output.
A 4xAA battery pack works for 1-2 servos. Don't forget to connect the grounds.
Finally, where did you get the library for the ultrasonic sensor? It is possible that there is a conflict between it and the servo library (both might use Timer1, for example).
Northof49:
Completely agree regarding the power supply.
I switched to driving my servos by I2C using a pca9685 board because I inevitably found that the Arduino produced glitches in servos that should be stationary. Some servos are more sensitive to it than others. It definitely gets worse with some Arduino libraries. That might be fine for some applications, but in others, like steadying a camera, it isn't. Nor is it anything you notice if you are moving the servos.
The ultrasonic sensors seem to be very sensitive to power supply issues. I find that even with a good power supply, hooking up a sharp IR sensor increases the rate of errors in reading ultrasonic values. Very frustrating.
I don't think that the problem is the library because I made my own mesuaring system and the same glitch happened.
I will try to power me servo from a different source. Would a cellphone charger be ok to do it? Or do I have to use batteries?
The servo power supply must be able to supply 5 to 6V at 1 Ampere (or more) current per servo.