1. Parts & Supplies
Round ‘em up!
- Arduino Uno
- Breadboard
- USB A-B cable
- Potentiometer
- Photoresistor
- LEDs
- Resistors (330 Ω and 10K Ω)
- Hookup wire
2. Wire it up!
Connect 5V and ground from the Arduino to your breadboard.
Connect the potentiometer – red wire to power (+), black wire to ground (GND), and the middle wire to analog input 0 on the Arduino. Note that this is the analog pin, not digital.
Connect an LED – cathode (-) to ground, anode (+) to a 330 ohm resistor, and the other side of the resistor to pin 9. Note that this is one of the pins marked “PWM”.
3. Brighten & Dim
Copy and paste the following code into a new Arduino sketch.
int LEDpin = 9; int potentiometerPin = 0; int LEDvalue; int potentiometerValue; void setup(){ pinMode(LEDpin, OUTPUT); } void loop(){ potentiometerValue = analogRead(potentiometerPin); LEDvalue = map(potentiometerValue, 0, 1023, 0, 255); analogWrite(LEDpin, LEDvalue); }
Under tools, check that you have the correct Board Type and Serial Port selected. Upload your program to the Arduino board. Once the program has been uploaded, you should be able to brighten and dim the LED with your potentiometer.
4. Look at your values
Within “setup”, add the following:
Serial.begin(9600);
And within the loop, add:
Serial.println(potentiometerValue);
Save and upload your program once more. Click the Serial Monitor icon in your menu bar. A new window should open where you will be able to see the changing values of your potentiometer.
5. Other variable resistors
Wire up your photo resistor or another variable resistor using the voltage divider circuit shown in class.
Once again, open your Serial Monitor to see the range of values you are getting. Adjust the parameters of the map() function so that the photoresistor can be used to control the LED.
6. Play with Thresholds
Modify your program so that the LED turns on only when the photo resistor senses a certain level of darkness. For instance:
if(potentiometerValue<500){ digitalWrite(LEDpin, HIGH); }else{ digitalWrite(LEDpin, LOW); }
7. Other examples:
Try these and write about your results:
- Analog Input: use a potentiometer to control the blinking of an LED.
- Calibration: for analog sensor readings
- Fading: uses an analog output (PWM pin) to fade an LED.
- Smoothing: smooth multiple readings of an analog input.
7. Make your own.
Using what you’ve learned above, create something with a sensor(s) and output(s) that responds differently to varying conditions. It can be as practical or wacky as you like. Be sure to include your code in your blog post and credit any code that your modified. Have fun!
8. Follow Up Reading
- Read Chapters 9 & 11 in “Physical Computing” to learn about more sensors.
- Spend some time looking at the “Sensors” section on the Sparkfun website
- Optional: Read about ideas for how to create your own variable resistors.