Brandon Roots
  • INTERACTIVE
  • FILM
  • ABOUT
  • CONTACT
September 18, 2020

Lab: Analog In with an Arduino

broots ITP, Physical Computing

In this lab I created two different circuits making use of analog inputs on the Arduino. Analog inputs are able to read a range of voltage inputs from zero to the voltage of the board which in the case of the Arduino Nano 33 IoT is 3.3 volts.

The goal of my first circuit was to read the value of a dial (potentiometer) and use that to adjust the brightness of an LED. Here is the wiring and sample code:

  • Arduino nano on breadboard with potentiometer and red LED
const int ledPin = 9;       // pin that the LED is attached to
int analogValue = 0;        // value read from the pot
int brightness = 0;         // PWM pin that the LED is on.

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  // declare the led pin as an output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
    analogValue = analogRead(A0);      // read the pot value
    brightness = analogValue /4;       //divide by 4 to fit in a byte
    analogWrite(ledPin, brightness);   // PWM the LED with the brightness value
    Serial.println(brightness);        // print the brightness value back to the serial monitor
}

This worked as expected! Pictures below.

  • Hand touching dial of potentiometer
  • Turning potentiometer adjusts brightness of red LED

This next circuit proved to be a bit more challenging. The goal here was to read values from two touch sensors, which vary output based on force applied, and map each of those readings to an LED to adjust its brightness.

Here is the setup on my breadboard:

  • Two touch sensors wired to Arduino nano

Making use of the provided sample code resulted in some odd behavior of the LEDs which remained on continuously. Though the brightness of the LEDs did vary when touching the sensors I thought a better behavior would be for them to turn off completely when no touch was registered.

  • Both green and red LEDs lit with power supplied to Arduino
  • Touching one sensor increases brightness of red LED

After watching the values of the touch sensors read out over the serial monitor I expected the brightness variable to return to zero when not being touched. Reading out the brightness variable values to the serial monitor I noticed that the mapping was inverting the values on the low end into negative numbers which was causing the LEDs not to dim completely. To resolve this issue I made some changes to the code below. Since the brightness variable in the main loop was occasionally returning a negative value I added an IF statement following each mapping function to set the brightness to “0” if the value is less than zero. While I was at it I also adjusted the mapping ranges and added a delay in the loop to smooth out the responsiveness. Issues fixed! Images and final code below:

  • Improved code allows LEDs to turn off completely when sensors not touched
  • Finger touching sensor lights up red LED
  • Touching other sensors lights up green LED
const int greenLED = 10;     // pin that the red LED is on
const int redLED = 11;   // pin that the green LED is on
int rightSensorValue = 0;  // value read from the right analog sensor
int leftSensorValue = 0;   // value read from the left analog sensor

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  // declare the led pins as outputs:
  pinMode(redLED, OUTPUT);
  pinMode(greenLED, OUTPUT);
}

void loop() {
  rightSensorValue = analogRead(A0); // read the pot value
  // map the sensor value from the input range (400 - 900, for example)
  // to the output range (0-255). Change the values 400 and 900 below
  // to match the range your analog input gives:
  int brightness = map(rightSensorValue, 10, 1000, 0, 255);
  if (brightness < 0) {brightness = 0;}
  analogWrite(redLED, brightness);  // set the LED brightness with the result
  Serial.println(rightSensorValue);   // print the sensor value back to the serial monitor
  // now do the same for the other sensor and LED:
  leftSensorValue = analogRead(A1); // read the pot value
  // map the sensor value to the brightness again. No need to
  // declare the variable again, since you did so above:
  brightness = map(leftSensorValue, 10, 1000, 0, 255);
  if (brightness < 0) {brightness = 0;}
  analogWrite(greenLED, brightness);  // set the LED brightness with the result
  Serial.println(leftSensorValue);   // print the sensor value back to the serial monitor
  delay(100);
}
Lab: Digital Input and Output with an Arduino New Skill: Regular Expressions

Related Posts

Fractal Plant – Foiled by  Registers

Homemade Hardware, ITP, Solar Plant

Fractal Plant – Foiled by Registers

Since receiving the PCBs and successfully soldering the board together I have been trying to rewrite code for the I2C port expander. This has been immensely difficult! The Inkplate Arduino Library makes considerable use of an “Mcp” class, which is written to work with the MCP23017 GPIO expander IC. These chips are quite difficult to […]

“Handling” Playtest Week

Handling, ITP

“Handling” Playtest Week

Last week we attended “Playtest Thursday” on the second floor of 370 Jay St with our games. I came away from the experience with some very specific feedback. Seeing a number of people play the game showed me things I didn’t anticipate. Some folks approached the cabinet and immediately treated it as a touch screen. […]

Fractal Plant – Beta Build

Homemade Hardware, ITP, Solar Plant

Fractal Plant – Beta Build

The boards arrived! Amazingly within an hour of one another. Based on the experience I think that JLCPCB is a better value. With shipping OSHPark was $55.50 for 3 boards. JLCPCB was $26.36 for 10 boards. Aside from a higher cost OSHPark also left sharp bits of tabs around the edges of the boards which […]

Recent Posts

  • Fractal Plant – Foiled by  RegistersFractal Plant – Foiled by Registers
    May 9, 2022
  • “Handling” Playtest Week“Handling” Playtest Week
    May 5, 2022
  • Fractal Plant – Beta BuildFractal Plant – Beta Build
    April 24, 2022
Brandon Roots