my goal is to read a pwm signal from a rc receiver. For that i tried to use an arduino.
i ran this code on the arduino:
#define Pin 3
int pwm_value;
void setup() {
pinMode(Pin, INPUT);
Serial.begin(9700);
}
void loop() {
pwm_value = pulseIn(Pin, HIGH);
Serial.println(pwm_value);
}
Now i connected the 3rd pin with the output of the receiver. But instead of printing the correct pwm values they get printed completely crazy. Sometimes they are under 10 and sometimes they are over 10000. The receiver outputs the correct signal.
I also noticed that if i just touch the pin with my finger or if i only connect a single jumper cable without anything at the end, the arduino will read a pwm signal.
I have realy no idea to solve that. Can you please help me?
Thank you!
1 Answer 1
check if the problem is the fact you have set 9700
baud rate on your sketch while the commonly used 9600
is supported from arduino IDE.
I slightly modified your code and it works for me. Check if this works for you:
#define Pin 3
#define Source 5
unsigned long pwm_value;
void setup() {
pinMode(Source,OUTPUT);
pinMode(Pin, INPUT);
Serial.begin(9600);
analogWrite(Source,500);
}
void loop() {
pwm_value = pulseIn(Pin, HIGH);
Serial.println(pwm_value);
}
when you connect pin3 with pin 5 you should get a consistent value on Serial(=967).
About getting signal on floating pins
Getting a signal on a floating pin is normal. When you have a cable attached it acts like an antenna. When you touch it, you are the antenna!
-
the serial connection over 9700 was the problem. with 9600 everything worked just fine. Thanks so much for the answer!CarringDO– CarringDO2016年05月09日 18:20:19 +00:00Commented May 9, 2016 at 18:20
pinMode(pin, INPUT_PULLUP);