Using a MotorShield
Parts required:
- Arduino Uno
- USB cable
- Motor Shield
- 6V battery pack
- small screwdriver
- 2 DC geared motors
- 1 stepper motor
- 1 servo motor
While it is possible to run motors directly from the Arduino - the power output limitations of the Arduino does not make this a practical option for most applications. The better option is to use the Arduino to send commands to another board - a motor shield - that is designed specifically for powering motors.
The particular shield that will be used is the Adafruit Motor/Stepper/Servo Shield for Arduino v2 Kit - v2.3.
The particular shield that will be used is the Adafruit Motor/Stepper/Servo Shield for Arduino v2 Kit - v2.3.
For the first part of this lab you should familiarize yourself with this device and run through simple tests whereby you control
two DC motors
a stepper motor
a servo motor
For all of these operations you will need to provide an external power source to the shield which will be used to provide the needed current to run the motors. This will be accomplished using a 6V battery pack that contains 4 AA batteries.
To begin your exploration of this device you will need to download and install the motor shield library.
The instructions on how to do this and some basic operational tutorials are located on the Adafruit site.
The instructions on how to do this and some basic operational tutorials are located on the Adafruit site.
Sensors that provide an analog input signal
Parts required:
- Arduino Uno
- USB cable
- breadboard
- potentiometer
- Photoresistor (LDR)
- 10k resistor
- Sparkfun line sensor module
Review the material on how to set up a potentiometer so that it can provide an analog voltage signal from 0 - 5 V. You should then set up and test your understanding by running the Arduino Example code File/Examples/01.Basics/AnalogReadSerial.
Once you have verified that you can use the Arduino to read an analog signal set up a simple circuit using the photoresistor that can measure the level of ambient light in the room. You can again refer to a tutorial from Adafuit.
Once you have completed the test with the photoresistor, turn your attention to the Sparkfun line sensor. This sensor works on a principle similar to the photoresistor except that the sensor is tuned to detect a narrow wavelength band in the IR spectrum. The sensor is composed of two devices - an IR LED that emits in a narrow wavelength and a detector that is tuned to that wavelength.
The setup of the sensor is quite simple...
And you can use the AnalogReadSerial example code to test the sensor.
Other things to try...
Using an IR remote to Control an Arduino Project
For some projects it is desirable to be able to control the system remotely. One easy way to do this is via an IR remote control.
In order to accomplish this you need to obtain an IR receiver module. These are rather inexpensive components and can be purchased or scavenged from old VCRs. DVD's and other remote control devices. If you wish to purchase you can do so from Sparkfun or Adafruit.
In order to accomplish this you need to obtain an IR receiver module. These are rather inexpensive components and can be purchased or scavenged from old VCRs. DVD's and other remote control devices. If you wish to purchase you can do so from Sparkfun or Adafruit.
The IR receiver module described here is the TSOP38238 purchased from Adafruit.
The Adafruit site has a nice tutorial on setting up and using the receiver.
For the connections to the Arduino: Vcc goes to +5 V, Vout goes to a digital input Pin.
For the examples that I will be using, the IR Receiver is connected to P11 and three LED's are connected to digital pins P2, P3, and P4. The final configuration is shown here:
Step 1: Download the library from GitHub and install the library using the following directions:
Step 2: Test system
As an initial test of your system your should upload and run one of the example sketches that comes with the IR Remote Library. File -> Examples -> IRremote -> IRrecvDemo. Running this sketch will allow you to both verify that you have correctly configured your IR Receiver module and obtain the IR codes from whatever IR Remote control that you wish to use.
After loading and uploading the sketch, open the Serial Monitor and then push a button on your remote control. If the system is working, you should see something similar to this...
You should notice that every time you push a button, you will see a different sequence of letters and numbers. If you hold down the key you will see a repeat code of FFFFFFFF. The library is decoding the IR signal and outputting a value in Hexadecimal format. If you want to check out how Hexadecimal (base 16) compares to both decimal (base 10) and binary (base 2) you can look at this online converter.
The Arduino code will recognize a hexadecimal value as a regular integer, however for these values you need to use the variable identifier long instead of int because these 8 digit hexadecimal numbers are larger than the upper limit of the int range. In decimal format FFFFFFFF is 4,294,967,295.
If all is working correctly, you can go ahead and note down the codes for the remote control keys that you want to use. For the case of my remote control, I wanted to use the first four digits on the number pad and the codes for these for keys are as follows:
button 1 - FF728D
button 2 - FF52AD
button3 - FF929D
button4 - FFB24D
In order to use these as hexadecimal numbers in the Arduino code, it is necessary to prepend an identifier so that the code does not mistake these values as a string or some other variable name. The identifier is 0x so the variables that we will use in the sketch become:
button 1 - 0xFF728D
button 2 - 0xFF52AD
button3 - 0xFF929D
button4 - 0xFFB24D
Step 3: Understand the particular lines that are required to make IRremote function.
Before presenting the entire sketch it may be helpful to look at the specific lines of code that relate to the IRremote library (some of my comments have been take from this page)
#include <IRremote.h> This line is place at the beginning of the sketch and alerts the Arduino IDE that it should include the IRremote library when the sketch is compiled.
int RECV_PIN = 11; This sets a variable name for the digital pin that is connected to the Vout pin of the IR receiver module. The example code uses P11, but any digital I/O pin can be used.
long button1 = 0xFF728D; This is one of the variable declarations that provides a variable name to the hexadecimal number that corresponds to the buttons on the remote. Note that the variable identifier long has been used instead of int.
IRrecv irrecv(RECV_PIN); IRrecv is a function that is defined by the IRremote library. This creates a IRrecv object that is called irrecv and this object is connected to RECV_PIN which has been defined as P11. Note that irrecv is a user defined name and you could use "bob" or another descriptive name. The use here is analogous to creating a Servo object when using the Servo library.
decode_results results; This line creates a memory location that is used to store the information
received from the decode function of IRremote
irrecv.enableIRIn()
if (results.value == button1){digitalWrite(ledBlue, HIGH);} This if statement compares the value that has been stored in results to the first of the four button codes
irrecv.resume()
Step 4: Create sketch to respond to specific IR signals from remote
The video and code show a simple application of the IRemote library. Four buttons are defined and buttons 1, 2, and 3 each turn on one LED. button 4 then turns all the LED's off.
(Video)
(sketch)
The Adafruit site has a nice tutorial on setting up and using the receiver.
For the connections to the Arduino: Vcc goes to +5 V, Vout goes to a digital input Pin.
For the examples that I will be using, the IR Receiver is connected to P11 and three LED's are connected to digital pins P2, P3, and P4. The final configuration is shown here:
Download and Install IR Remote Library
The library that I recommend was written by Ken Shirriff. He has a full description of the project on his blog under the post A Multi-Protocol Infrared Remote Library for the Arduino.Step 1: Download the library from GitHub and install the library using the following directions:
Step 2: Test system
As an initial test of your system your should upload and run one of the example sketches that comes with the IR Remote Library. File -> Examples -> IRremote -> IRrecvDemo. Running this sketch will allow you to both verify that you have correctly configured your IR Receiver module and obtain the IR codes from whatever IR Remote control that you wish to use.
After loading and uploading the sketch, open the Serial Monitor and then push a button on your remote control. If the system is working, you should see something similar to this...
You should notice that every time you push a button, you will see a different sequence of letters and numbers. If you hold down the key you will see a repeat code of FFFFFFFF. The library is decoding the IR signal and outputting a value in Hexadecimal format. If you want to check out how Hexadecimal (base 16) compares to both decimal (base 10) and binary (base 2) you can look at this online converter.
The Arduino code will recognize a hexadecimal value as a regular integer, however for these values you need to use the variable identifier long instead of int because these 8 digit hexadecimal numbers are larger than the upper limit of the int range. In decimal format FFFFFFFF is 4,294,967,295.
If all is working correctly, you can go ahead and note down the codes for the remote control keys that you want to use. For the case of my remote control, I wanted to use the first four digits on the number pad and the codes for these for keys are as follows:
button 1 - FF728D
button 2 - FF52AD
button3 - FF929D
button4 - FFB24D
In order to use these as hexadecimal numbers in the Arduino code, it is necessary to prepend an identifier so that the code does not mistake these values as a string or some other variable name. The identifier is 0x so the variables that we will use in the sketch become:
button 1 - 0xFF728D
button 2 - 0xFF52AD
button3 - 0xFF929D
button4 - 0xFFB24D
Step 3: Understand the particular lines that are required to make IRremote function.
Before presenting the entire sketch it may be helpful to look at the specific lines of code that relate to the IRremote library (some of my comments have been take from this page)
#include <IRremote.h> This line is place at the beginning of the sketch and alerts the Arduino IDE that it should include the IRremote library when the sketch is compiled.
int RECV_PIN = 11; This sets a variable name for the digital pin that is connected to the Vout pin of the IR receiver module. The example code uses P11, but any digital I/O pin can be used.
long button1 = 0xFF728D; This is one of the variable declarations that provides a variable name to the hexadecimal number that corresponds to the buttons on the remote. Note that the variable identifier long has been used instead of int.
IRrecv irrecv(RECV_PIN); IRrecv is a function that is defined by the IRremote library. This creates a IRrecv object that is called irrecv and this object is connected to RECV_PIN which has been defined as P11. Note that irrecv is a user defined name and you could use "bob" or another descriptive name. The use here is analogous to creating a Servo object when using the Servo library.
decode_results results; This line creates a memory location that is used to store the information
received from the decode function of IRremote
irrecv.enableIRIn()
Begin the receiving process. This will enable the timer interrupt which consumes a small amount of CPU every 50 µs.
irrecv.blink13(true)
Enable blinking the LED when during reception. Because you can't see infrared light, blinking the LED can be useful while troubleshooting, or just to give visual feedback.
irrecv.decode(&results)
Attempt to receive a IR code. Returns true if a code was received, or false if nothing received yet. When a code is received, information is stored into "results".
results.decode_type: Will be one of the following: NEC, SONY, RC5, RC6, or UNKNOWN.
results.value: The actual IR code (0 if type is UNKNOWN)
results.bits: The number of bits used by this code
results.rawbuf: An array of IR pulse times
results.rawlen: The number of items stored in the array
results.value: The actual IR code (0 if type is UNKNOWN)
results.bits: The number of bits used by this code
results.rawbuf: An array of IR pulse times
results.rawlen: The number of items stored in the array
if (results.value == button1){digitalWrite(ledBlue, HIGH);} This if statement compares the value that has been stored in results to the first of the four button codes
irrecv.resume()
After receiving, this must be called to reset the receiver and prepare it to receive another code.
Step 4: Create sketch to respond to specific IR signals from remote
The video and code show a simple application of the IRemote library. Four buttons are defined and buttons 1, 2, and 3 each turn on one LED. button 4 then turns all the LED's off.
(Video)
(sketch)