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 use a Processing application to control a servo motor hooked up to an Arduino. You will control the movement of a servo motor by dragging a red circle left and right on your yellow computer screen.
The circuit used simply involves a servo motor connected to one of the Arduino’s analog output pins. Our code assumes we are using pin 5. If you need a refresher on how to connect a servo motor to an Arduino, check out this simple example of a servo controlled from Arduino code.
The Processing code
The Arduino code
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.
In this lab, you will control a servo motor from a pushbutton switch. Holding down the momentary pushbutton switch turns the servo motor incrementally until it reaches its upper or lower limit (180° or 0°, respectively).
Once the servo reaches its upper or lower limit, it continues in the opposite direction.
- Design it yourself. There is no circuit for you to follow. -
- Write it yourself. There is no code for you to copy. -
This simple example shows how to oscillate a servo motor back and forth automatically. Servos respond to the pulse width modulation (PWM) of a signal, meaning that changes in the width of DC current pulses cause changes in the rotation of the servo’s armature.
Whereas most applications of PWM deal with the duty cycle of the pulses (i.e. the relative duration of the signal, as compared to the duration of no signal), servos respond to the absolute width of the pulses. In other words, a pulse of a particular duration will always move the servo to a particular rotation angle, regardless of how much time exists between pulses (within limitations, of course).
- Why do we add a delay after rotating the servo?
- What is the pulse width that corresponds with a 45° rotation?
- Why doesn’t the Arduino reliably turn a servo motor at the same time as it plays a sound from a piezo speaker using the tone() function?
This example shows how to hook up a transistor so that it can be controlled by an Arduino. The Arduino can either use the transistor as a low-power switch to turn on higher power loads, or as an analog amplifier for converting small amplitude signals to larger amplitude signals. There are examples of code for both uses.
Note: These examples use 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.
Transistor as switch
The following code uses the transistor as a switch by sending current directly to the base of the transistor.
Transistor as amplifier
The following version of the code uses the transistor as an amplifier by pulsing the base of the transistor at varying degrees in order to simulate an analog signal.
Note: unlike the example of using a transistor as an amplifier based on an analog source signal , the transistor in this example is not truly behaving in an analog fashion. That is because the Arduino is not capable of producing truly analog signals. The transistor in this example is actually being used as an on/off switch, but the switch is being pulsed on and off very quickly at a varying frequency in order to simulate an analog signal.
This circuit uses a temperature sensor to sense the ambient temperature of the room. This data is fed into an Arduino, which then pulses an LED in proportion to the current room temperature.
This temperature sensor (TMP36) is an integrated circuit chip that changes its output voltage in proportion to temperature changes. The chip outputs a range of voltages from 0.1V at -40°C, to 2.0V at 150°C. So, we can hook up its output directly to one of the Arduino’s analog inputs, which expect to receive varying voltages as the incoming signal.
- We use a voltage divider circuit for potentiometers, force-sensitive resistors, and many other sensors. Why do we not need to use a voltage divider circuit with this temperature sensor?
- Unlike pressure sensed by force-sensitive resistors, or rotation sensed by potentiometers, ambient temperature sensed by temperature sensors almost always changes very gradually and slowly. How does this affect the design of interactions based on this sensor?
- Given the range of temperatures at which this sensor works, please give 2 ideas for applications of this sensor. Applications may involve anything from scientific research to artistic endeavors, but they should necessitate the use of this sensor, as opposed to other sensors.
This circuit feeds data from a force-sensitive resistor into the Arduino, using a classic voltage divider circuit. The Arduino, then pulses out to an LED in proportion to how much force was exerted.
The Arduino code makes use of the Arduino map() function, which maps a number from one range to another. In this case, we are mapping a number from 0-1023 (the range corresponding to analog input on the Arduino) to its corresponding number from 0-255 (the range corresponding to analog output on the Arduino).
Note: This is almost exactly the same circuit as that used for the Potentiometer and LED example. All sensors that work via variable resistance will use this same voltage divider technique, although the exact value of the resistor used will depend upon the specific resistance range of the variable resistor being used.
- Give ideas of 2 applications of this sensor in the field of improving accessibility for handicapped people.
This example shows how to read a photoresistor using the Arduino, and use its value to change the frequency of sound coming out of a speaker. Open the Serial Monitor window in the Arduino software to view the debugging messages output by the program.
- How would you change the circuit (without altering the code), so that the behavior of the sound in relation to the light falling on the photoresistor was reversed?
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?