In this lab, you will use a potentiometer to control the speed of a toy DC motor from an Arduino.
A typical circuit for controlling a DC motor from Arduino would be something like the following. Note that this circuit assumes you are using a small enough motor that it can be run from a 5V power supply. Larger motors will need their own power supplies.
You will have to develop your own Arduino code and your own potentiometer circuit in order to complete this lab.
- make your own circuit and your own code
- read the potentiometer into Arduino, and output a PWM signal to the motor based on that reading
- see your ARDX kit motor documentation if you get stuck
This example shows how to send data from an Arduino circuit to a Processing application via serial communication. Serial communication is a very common and standardized protocol that allows two computers or devices to talk to each other via a series of digital pulses over a few wires (in this case a USB cable).
In this example, we program the Arduino to read the analog value of a potentiometer plugged into a simple circuit. The Arduino then relays this potentiometer value to the desktop computer using serial communication over the USB cable. The Processing application on the computer then reads this serial data and uses it to control the diameter of a red circle on the yellow screen.
You must build a circuit where a potentiometer is connected to one of the analog input pins of the Arduino. The code assumes this will be pin A0.
If you have forgotten how to hook up a potentiometer to an Arduino, then perhaps reviewing a simple example of a reading potentiometer’s value in Arduino to control an LED will help.
The Arduino code
Notice that we use the Serial.write() function of the Arduino to send the potentiometer reading out the serial port to the desktop computer.
The Processing code
Notice that we import a library that contains a lot of useful functions for dealing with serial communications. Once we have read in the serial data into the Processing code and have thereby obtained the potentiometer reading send from the Arduino, we use it to control the diameter of an ellipse which we continually draw to the screen.
This example shows how to control a servo motor by turning a potentiometer. The Arduino detects the changes to the rotation of the potentiometer and then maps those changes to rotations of the servo.
Other types of variable resistors can easily be used in place of the potentiometer, by using the variable resistor in a Voltage Divider circuit.
In addition to being used as switches, transistors can also be used as amplifiers. Using a transistor this way, a low power signal is effectively translated into a higher power signal. The following example exhibits this behavior.
In this example, the photo-resistor is used as a variable resistor. This photo-resistor resistance is used in a classic Voltage Divider circuit to feed an analog signal into the base of the transistor. The transistor then opens the flow of current through the LED in proportion to how much current is flowing into the transistor base. The potentiometer is included in order to allow you to tune the sensitivity of the voltage divider circuit for different light conditions.
Note: This example uses the Arduino 5V pin as the power supply and LEDs as the “load” for simplicity. The power coming out of the Arduino 5V pin (max 40mA) is generally too small to drive much more than a few LEDs or a very small motor. For larger loads, such as bigger motors or relays, or stronger lights or sound, a stronger power supply, such as from batteries or a power adaptor, would be used instead.
This type of circuit is called using a transistor in “common emitter mode”, since the base->emitter circuit and collector->emitter circuits both connect current to ground at the same emitter.
- How does turning the potentiometer to maximum resistance affect the sensitivity of the circuit to the photo-resistor’s signal, as compared to turning the potentiometer all the way to minimum resistance?
This example shows how to read a potentiometer using the Arduino and use its value to change the frequency of a sound coming out of a speaker. Open the Serial Monitor window in the Arduino software to view the debugging messages output by the program.
- The speaker is connected directly to power and ground… Isn’t that a short circuit and therefore dangerous?
- The sound produced by this circuit is not very loud. What would you have to do to get the sound louder?
- Are we having fun yet?
This example shows how to read a potentiometer using the Arduino and use its value to change the brightness of an LED. Open the Serial Monitor window in the Arduino software to view the debugging messages output by the program.
- When the potentiometer is turned all the way down (offering minimum resistance), what voltage is read at Arduino pin A0?
- When the potentiometer is turned all the way up (offering maximum resistance), what voltage is read at Arduino pin A0?
- How much resistance must the potentiometer offer in order for the voltage at Arduino pin A0 be 2.5V?
- Why have I multiplied the potentiometer value (potVal in the code) by 0.25 in order to set the brightness of the LED (ledVal in the code)? In other words, why couldn’t I just set the LED output value to be exactly the same value as the potentiometer input value?
The following circuit contains two LEDs whose brightness is controlled by the same potentiometer.
- Give a quick (1-2 sentences) explanation of why this circuit behaves the way it does. Include an explanation for how the current going through each LED is affected by a single turn of the potentiometer
The following circuit has an LED wired through a potentiometer, which is a form of a variable resistor (a resistor who’s value can change).
- Why is the 560Ω resistor necessary?
- How much current is flowing through the circuit when the 10kΩ potentiometer is turned all the way up (offering the maximum resistance)?
- What other types of variable resistors do you know about?