![]()
Lab: Analog In with an Arduino
For the 2nd Week into Intro to Physical Computing, my labwork involved two parts. Part 1: Connecting a variable resistor to a microcontroller and reading it as an analog input. FYI, a variable resistor allows the user to control the flow of voltage from zero to maximum output. By doing so, variable resistors can be used to make a digital output appear analog (dimming lights, etc) by use of it’s handheld twist nob to adjust settings.
Notes from the Lab:
1. Don’t breathe in solder fumes when having to solder wiring to the Potentiometer (variable resistor listed on bottom of photo). Bad.
2. For analog output, Analog Pin Slots (0-5) are proper places to stick wires in. Likewise, you connect a potentiometer to analog in pin 0 of the module.
3. To create a “fake” analog output, a wire must be threaded to one of the Digital Pin Slots (9, 10, 11) as indicated as PWM (Pulse Width Modulation). That way, signals can be controlled in pulses that simulate analog outputs (rather then digital output’s 1=ON, 0=OFF). Here, we linked an LED to digital pin 9:
I made some handy infographics that show how the Arduino Code relates to the circuitry with the BreadBoard here. Click to view.
NYU Intro to Physical Computing Lab: Analog In with an Arduino from cindy wong on Vimeo.
Deciphering Arduino terminology:
analogWrite():
- “The pins marked PWM (pins 9 – 11 on Arduino, PWM 0 – 5 on Wiring) can be pulsed using the analogOut() command.
analogWrite(pin, pulsewidth); — Tom Igoe in Analog In with an Arduino Lab.
- analogVar is an integer variable containing the result from the ADC (analog-to-digital converter).
- The command in Wiring is the analogRead() command. It looks like this: analogVar = analogRead (pin).
- Pin is the analog input pin you are using;
- analogVar is an integer variable containing the result from the ADC.
int potPin = 0; // Analog input pin that the potentiometer is attached to
int potValue = 0; // value read from the pot
int led = 9; // PWM pin that the LED is on. n.b. PWM 0 is on digital pin 9
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
potValue = analogRead(potPin); // read the pot value
analogWrite(led, potValue/4); // PWM the LED with the pot value (divided by 4 to fit in a byte)
Serial.println(potValue); // print the pot value back to the debugger pane
delay(10); // wait 10 milliseconds before the next loop
}
Part 2: Read changing conditions from the physical world and convert them to changing variables in a program.
What I used: Photo Cell, LED.
Cause & Effect relationship: Rig the photo cell to act as a touch sensor “dimmer” for the LED incorporated into the Arduino breadboard.
Video below:
Intro to Physical Computing: Photo Cell, LED experiment from cindy wong on Vimeo.
Here’s the initial breadboard setup with 1 LED light interacting with a photo sensor.
![]()
Update: Added an extra light and caused both to light and dim simultaneously.
![]()
Red and Clear Light Sensors Dimmed from Fingers placed on top of sensor.
![]()
Red and Clear Light Sensors brightened without interference from me.
Here’s another handy infographic I made to relay the information of the Arduino to the BreadBoard.
Arduino Code Used:
int potPin = 0; // Analog input pin that the potentiometer is attached to
int potValue = 0; // value read from the pot
int led = 9; // PWM pin that the LED is on. n.b. PWM 0 is on digital pin 9
int led2 = 10; // SECOND PWM pin that the LED is on. n.b. PWM 0 is on digital pin 10
int x = 0; //initiate the variable
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
potValue = analogRead(potPin); // read the pot value
x = potValue/4;
analogWrite(led, potValue/4); // PWM the LED with the pot value (divided by 4 to fit in a byte)
analogWrite(led2, potValue/4); // PWM the LED with the pot value (divided by 4 to fit in a byte (256))
Serial.println(potValue); // print the pot value back to the debugger pane
delay(10); // wait 10 milliseconds before the next loop (streams the rate of binary numbers appearing in debugging panel)
}