How to use an Arduino to read a PWM signal

Rusty Tue, 01/24/2017 - 18:16

How to use an Arduino to read Pulse Width Modulation

This is just a quick little 'how to' to show how to read the PWM signal from a RC receiver using an Arduino, in this case and Arduino UNO, but a Tiny would be better. I am just using the UNO for development and it is easier to show on the UNO. There are dozens of uses for this, so the end use is up to you.

What do we need?

Other than the Arduino, you probably have everything, but I'll list what I used.

  1. Arduino UNO, and a way to power it. A BEC connected to a RC receiver can do this.
  2. A way to program the Arduino and connect to to your PC
  3. Servo cable
  4. Male/Female servo plug and pins

That's it, other than a source for the PWM signal, but you probably have that covered.

Wiring up the Arduino to read PWM

Wiring up the Arduino is fairly simple, it's only 3 wires, it is the same 3 wires that a standard RC servo uses to connect to a RC receiver. The wires are: 5V, GND, and Signal. The 5V and ground connect to the 5V and GND pads on the Arduino and the Signal wire connects to one of the digital I/O Pins. If you intend to run this with the USB attached, as we are going to do for this demonstration, DO NOT connect the 5V wire. See image below








The Sketch

The sketch for the Arduino is relatively simple. What we are going to do is use the 'pulseIn' command to measure the with of the incoming pulse on digital I/O pin 7. In this sketch I am also using the on board LED to indicate if the pulse width is below or above the 1500us value.

// Simple sketch to read the pulse width of a PWM signal
// Useful for reading PWM valuss from a R/C reciever
// to create actions that are not typically handled
// with a servo, such as controlling LED lighting.

int pin = 6;  // Check what pin to use
int outblink = 13; // LED pin to blink
int duration; // Duration of the pulse

// setup serial and input, output pins
void setup()
  pinMode(pin, INPUT); // PWM input pin
  pinMode(outblink, OUTPUT); // LED Blink pin, using the build in LED on pin 13



void loop()

  duration = pulseIn(pin, HIGH);
  if (duration > 1500)
      digitalWrite(outblink, HIGH);
      digitalWrite(outblink, LOW);
  Serial.print("pulse ");
  delay(100); //delay so you can read the scrolling output


Loading and running the sketch

Basic Arduino processes are really out of the scope of this How to, but maybe in the future I will write something up.  But for this sketch we will assume you have basic Arduino familiarity. Load the sketch onto your Arduino and it should start running as soon as it is done loading. then start the Arduino serial monitor. A window will open and the Arduino should now be displaying the PWM value in a running column. If everything is running right you should see this value change as you vary the PWM input from the RC receiver (or servo tester).

What can you use this for?

I'll leave that up to you to figure out, but typical uses can be lighting control, sound effects, mechanical grippers? It s completely up to you.....


If you prefer, I made a short video of this in action, check it out below: