Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
12 years, 1 month ago.
Analog In PWM Out to control rc servo
I am trying to figure out how to move a servo motor when a light sensor is connected to a analog pin. I am using a infrared line tracker that has Vcc in, Gnd, and a Vout witch is the analog signal to the mbed Controler. What I would like it to do is when the sensor has light in front of it the PWM output will move a servo motor and stop when it reaches its limit of 180 degrees. I have been able to control the the input with the line tracker sensor to turn my led on. But to control a rc servo motor has been proven to be more difficult for me
3 Answers
12 years, 1 month ago.
I believe the mbed is a 3.3v device so it would be unwise to put 4.5v directly into it. Create a divider with two 10K resistors; connect the junction between them to your ADC input. Your threshold now becomes 2.25v which is safe in a 3.3v system.
include the mbed library with this snippet
#include "mbed.h"
PwmOut servo(p24);
int main() {
servo.period_us(20000); //20ms period, typical for analog RC servo
int servopulsewidth=1500;
servo.pulsewidth_us(servopulsewidth); //centers the servo. Usually a RC servos range is from 1000 to 2000us.
while(1){
servo.pulsewidth_us(servopulsewidth++); //increments servopulsewidth with 1
wait_ms(20);
}
}
12 years, 1 month ago.
If you posted your relevant source code, and perhaps a bit more specifics about your servo problem then we will be able to help you.
A RC servo is really simple though, here is an example code with a fictionary light sensor
RC servo
#include "mbed.h"
PwmOut servo(p24);
int main() {
servo.period_us(20000); //20ms period, typical for analog RC servo
int servopulsewidth=1500;
servo.pulsewidth_us(servopulsewidth); //centers the servo. Usually a RC servos range is from 1000 to 2000us.
while(1){
if(lightsensor==1){
servo.pulsewidth_us(servopulsewidth++); //increments servopulsewidth with 1
}
wait_ms(20);
}
}
Hopefully this will guide you to how to use a RC servo.
12 years, 1 month ago.
Thank you so much I think this is what I could be looking for. I will try it our for sure. one other question I have and you might be able to help me out, say I wanted the analog pin to read 4.5v from the sensor depending on how I power it. but I don't want it to put a out put on the pin till it reaches that voltage.