In this example we aim to accomplish one of the most common tasks: I have a small DC motor laying around, how do I use my Arduino to control it? Easy, with PWM and serial communication, using the function analogWrite()
and the Serial
library.
Pulse Width Modulation or PWM for short is a technique for mimicking analog signals using digital output. How does this work? Using a pulse train whose relation D (duty cycle) between time at high level (digital 1, usually 5V) and time at low level (digital 0, 0V) in each period can be modified to produce an average voltage between these two levels:
By using Arduino's analogWrite(pin,value)
function we can vary the value
of the duty cycle of pin
's output. Note that the pin
must be put into output mode and the value
must be between 0 (0V) and 255 (5V). Any value in between will simulate a proportional intermediate analog output.
However, the purpose of analog signals is usually related to the control of mechanical systems that require more voltage and current than the Arduino board alone is capable of. In this example, we will learn how to amplify Arduino's PWM capabilities.
For this a MOSFET diode is used. In essence, this diode acts as a switch. It allows or interrupts the electric flow between its source and drain terminals. But instead of a mechanical switch, it features a third terminal called gate. A very small current (<1mA) will "open" this gate and allow the current to flow. This is very convenient, because we can send Arduino's PWM output to this gate, thereby creating another PWM pulse train with the same duty cycle through the MOSFET, which allows voltages and currents that would destroy the Arduino.
Put everything together! Power the rails of the breadboard and place the MOSFET diode in it. Connect the motor between the positive rail and the MOSFET drain. Connect the protection diode in the same way: between the MOSFET drain and the positive rail. Connect the source of the MOSFET to the common ground rail. Finally, connect the PWM pin (we're using pin 10 in this example) to the gate of the MOSFET and also to the common ground through the resistor (we need very low current!).
Here's an example of how this build looks. If you prefer an scheme here's one.
Now we can connect the Arduino to a computer, upload the code and control the motor, by sending values through the serial communication. Recall that these values should be integers between 0 and 255. The actual code of this example is very simple. An explanation is provided in each line.
int in = 0; // Variable to store the desired value
byte pinOut = 10; // PWM output pin
void setup() { // This executes once
Serial.begin(9600); // Initialize serial port
pinMode(pinOut, OUTPUT); // Prepare output pin
}
void loop() { // This loops continuously
if(Serial.available()){ // Check if there's data
in = Serial.read(); // Read said data into the variable "in"
analogWrite(pinOut, in); // Pass the value of "in" to the pin
}
}
And that's it! Now you can use Arduino's PWM capabilities to control applications that require analog signals even when the power requirements exceed the board's limits.