Lab: Analog In with an Arduino
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:
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.
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:
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.
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:
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); }