Exercise One: Analog Mapping
Background Knowledge:
How to read a Schematic
How to use a Breadboard
How to read and use a Multimeter
Introduction to Arduino with TinkerCAD
What you'll need:
1 - 9 Volt Battery
1 - 9 Volt Battery Harness
1 - Breadboard
1 - Arduino UNO
1 - 100K-Ohm Potentiometer
1 - 470 Ohm Resistor
1 - 5mm Red LED
1 - Jumper Wire Set
In Arduino microcontrollers, analog inputs are constrained to a range of 0 to 1023 because they use a 10-bit analog-to-digital converter (ADC) to convert continuous analog voltage levels into discrete digital values.
A 10-bit ADC can represent a voltage range from 0 to 2^10 - 1, which is 0 to 1023. This means that the analog input value you read from an Arduino pin will be a number between 0 and 1023, representing the voltage level applied to that pin. The reference voltage for the ADC is typically the operating voltage of the microcontroller, which is usually 5V for most Arduino boards.
This limited range can be a bit coarse for some applications, but it's usually sufficient for many common tasks. If higher precision is needed, external hardware or specialized ADCs with higher bit resolutions can be used.
If you want to map this 0-1023 range to a different range (for example, if you're working with a sensor that outputs a voltage range not equal to 0-5V), you can use the map() function in Arduino to scale the readings to your desired range.
Steps
Set Up the Circuit: Begin by connecting the potentiometer and LED to the Arduino:
Connect the potentiometer to 5V+ and GND as shown to the right. Connect the center pin to the analog input A0.
Connect your LED to any digital output- in this case, pin 3. Be sure to put a resistor in series with the LED as to not break the LED.
2. Write the Arduino Sketch
The goal of this code is to understand mapping inputs. Create two variables for your analog input and your digital output. Assigning input and output pins to variables is best practice and will help you keep track of things in larger projects. On TinkerCAD, mapping is as simple as using the mapping block under the math blocks. Just map the variable you want to change to new values. In this case, we are mapping the 0 to 1023 of the analog input to 0 to 255, the PWM range for power output on an Arduino.
int sensorValue = 0;
int outputValue = 0;
void setup()
{
pinMode(3, INPUT);
pinMode(A0, INPUT);
pinMode(3, OUTPUT);
Serial.begin(9600);
}
void loop()
{
outputValue = digitalRead(3);
sensorValue = analogRead(A0);
outputValue = map(sensorValue, 0, 1023, 0, 255);
analogWrite(3, outputValue);
Serial.print("Sensor:");
Serial.println(sensorValue);
Serial.print("Output:");
Serial.println(outputValue);
delay(10); // Delay a little bit to improve simulation performance
}
3. Understand the Code
int sensorValue = LOW (0); stores the starting state of the potentiometer.
int outputValue = LOW (0); stores the starting state of the output which is later assigned to pin 3.
In the loop():
outputValue = digitalRead(3); Sets pin 3 to outputValue variable
sensorValue = analogRead(A0); Sets A0 to sensorValue variable
outputValue = map(sensorValue, 0, 1023, 0, 255); maps the analog sensor's range of 0 to 1023 to the output variable of 0 to 255 for PWM.
Serial.print; prints text and variable values for both
4. Upload and Monitor
Upload the sketch to the Arduino board. Open the Serial Monitor in the Arduino IDE (Ctrl+Shift+M or Tools > Serial Monitor). You will now see a continuous stream of the variables' values.
5. Experiment and Observe
Spin the potentiometer. The serial monitor will read information on the sensor and output. The LED should also range from 0% light to 100% light as you spin the potentiometer one way or another.
This lesson focuses on how you can map different sensors to link them to your outputs. These values can be changed for different sensors and outputs depending on need.
**Extra Experiment:
Consider what the code would look like to change the Sensor input reading and output reading to simply state the percentage of power the LED is on. In this case, you can output in plain English what the state of the LED is instead of needing to know what the analog an digital ranges of the Arduino are. This might be used on an LCD screen in a later project.
Questions:
How does analog mapping enhance the interpretation of potentiometer readings?
What other information can be useful to display in the Serial Monitor when working with analog mapping?
How do you interpret the data displayed in the Serial Monitor, especially after analog mapping?
How can you relate the original potentiometer readings to the mapped values?
How does this project lay the groundwork for more complex projects involving analog signals?
Exercise Two: Variable Blinking
Background Knowledge:
How to read a Schematic
How to use a Breadboard
Introduction to Arduino with TinkerCAD
What you'll need:
1 - 9 Volt Battery
1 - 9 Volt Battery Harness
1 - Breadboard
1 - Arduino UNO
1 - 100K-Ohm Potentiometer
1 - 470 Ohm Resistor
1 - 5mm Red LED
1 - Jumper Wire Set
Steps
Set Up the Circuit: Begin by connecting the potentiometer and LED to the Arduino just like you did above:
Connect the potentiometer to 5V+ and GND as shown to the right. Connect the center pin to the analog input A0.
Connect your LED to any digital output- in this case, pin 3. Be sure to put a resistor in series with the LED as to not break the LED.
2. Write the Arduino Sketch
This code should be relatively easy to write. You are using what you know about mapping to map your analog sensor to time in milliseconds instead of power for the output pin.
int blinkInterval = 0;
int Sensor = 0;
int Output = 0;
void setup()
{
pinMode(A0, INPUT);
pinMode(3, OUTPUT);
}
void loop()
{
Sensor = analogRead(A0);
blinkInterval = map(Sensor, 0, 1023, 50, 1000);
digitalWrite(3, HIGH);
delay(blinkInterval); // Wait for blinkInterval millisecond(s)
digitalWrite(3, LOW);
delay(blinkInterval); // Wait for blinkInterval millisecond(s)
}
3. Understand the Code
pinMode(ledPin, OUTPUT); sets the LED pin as an output.
In the loop():
int Sensor = analogRead(Sensor); reads the value from the potentiometer.
int blinkInterval = map(Sensor, 0, 1023, 50, 1000); maps the potentiometer reading (0-1023) to a delay range (50-1000 ms).
digitalWrite(ledPin, HIGH); turns on the LED.
delay(blinkInterval); waits for the specified interval.
digitalWrite(ledPin, LOW); turns off the LED.
delay(blinkInterval); waits for the specified interval again.
4. Upload and Monitor
Upload the sketch to the Arduino board. Open the Serial Monitor in the Arduino IDE (Ctrl+Shift+M or Tools > Serial Monitor).
5. Experiment and Observe
Spin the potentiometer. The LED will blink faster or slower depending on the proportion of the variable resistor's low and max in relationship to 50 ms and 1000 ms.
This project provides a hands-on demonstration of how a potentiometer can be used to dynamically control an aspect of an electronic circuit, in this case, the blink rate of an LED. This knowledge can be applied to various projects where adjustable parameters are required.
Questions:
How does the potentiometer interact with the LED in terms of controlling the blink speed?
How are variables initialized in the Arduino sketch for storing potentiometer readings and LED blink speed values?
What naming conventions are followed when creating variables in the sketch?
How does the choice of variable names contribute to code readability?
Can you think of applications where dynamic control of visual elements is valuable?
Can you modify the sketch to display both potentiometer readings and LED blink speed variables in the Serial Monitor?
Exercise Three: LED Fading Scale
Background Knowledge:
How to read a Schematic
How to use a Breadboard
Introduction to Arduino with TinkerCAD
What you'll need:
1 - 9 Volt Battery
1 - 9 Volt Battery Harness
1 - Breadboard
1 - Arduino UNO
1 - 100K-Ohm Potentiometer
1 - 470 Ohm Resistor
1 - 5mm Red LED
1 - Jumper Wire Set
Steps
Set Up the Circuit: Begin by connecting the potentiometer and LED to the Arduino just like you did above:
Connect the potentiometer to 5V+ and GND as shown to the right. Connect the center pin to the analog input A0.
Connect your LEDs to any digital output- in this case, pins 0-9. Be sure to put a resistor in series with the LED as to not break the LED. You can use one resistor to protect all the LEDs to save on parts.
2. Write the Arduino Sketch
In the Arduino IDE, this code would be much shorter and simpler because you can increment pin outputs- in TinkerCAD you cannot so your only option is to do Logic statements. This works fine but is cumbersome. For a sample of this less cumbersome code, check out the AnalogWriteMga code under Analog in the example library on Arduino Create.
This block code is very long and repetitive. Map your sensor from 0 to 10. You have 10 LEDs plus 0 for OFF. Your first logic statement should ask "If the LEDNumber is greater than or equal to 1, turn on. This will make sure the first LED only turns on if the variable is greater than 1 or exactly 1. The second through ninth logic statement can ask "If LEDNumber is greater than the LED# you want on. This will guarantee all the LEDs stay on even as the LEDNumber variable increases past the actual LED#. The last logic statement has to be unique in that it will only turn on if the LEDNumber variable is exactly 10 since that is the top limit of the mapping.
int Sensor = 0;
int Output = 0;
int LEDNumber = 0;
void setup()
{
pinMode(A0, INPUT);
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
...
pinMode(9, OUTPUT);
}
void loop()
{
Sensor = analogRead(A0);
LEDNumber = map(Sensor, 0, 1023, 0, 10);
if (LEDNumber >= 1) {
digitalWrite(0, HIGH);
} else {
digitalWrite(0, LOW);
}
if (LEDNumber > 2) {
digitalWrite(1, HIGH);
} else {
digitalWrite(1, LOW);
...
if (LEDNumber > 9) {
digitalWrite(8, HIGH);
} else {
digitalWrite(8, LOW);
}
if (LEDNumber == 10) {
digitalWrite(9, HIGH);
} else {
digitalWrite(9, LOW);
}
delay(10); // Delay a little bit to improve simulation performance
}
Note: Above is the abbreviated code. You will have to finish it to input it directly into the IDE.
3. Understand the Code
pinMode(ledPin, OUTPUT); sets the different LED pins as an outputs
In the loop():
int Sensor = analogRead(Sensor); reads the value from the potentiometer.
int blinkInterval = map(Sensor, 0, 1023, 0, 10); maps the potentiometer reading (0-1023) to a number of LEDs (0-10)
if (LEDNumber > 2) {
digitalWrite(1, HIGH);
} else {
digitalWrite(1, LOW); sets the LED to on if the sensor is in range, otherwise keeps it off.
4. Upload and Monitor
Upload the sketch to the Arduino board. Open the Serial Monitor in the Arduino IDE (Ctrl+Shift+M or Tools > Serial Monitor).
5. Experiment and Observe
Spin the potentiometer. A different amount of LEDs will urn on between 0 and 10 depending on the proportion of the variable resistor's low and max in relationship to the mapped 0 and 10 LEDs.
This project provides a hands-on demonstration of how a potentiometer can be used to dynamically control an aspect of an electronic circuit, in this case, the number of LEDs turned on. This knowledge can be applied to various projects where adjustable outputs such as a battery or power scale is required visually.
Questions:
How do the 10 LEDs contribute to creating a "fading scale" effect?
Why is there only one resistor for all the LEDs?
What functions or techniques are used to create the fading effect?
Can you modify the sketch to create different fading patterns or sequences?
Exercise Four: Flex Sensor (Understanding Voltage Drops)
Background Knowledge:
How to read a Schematic
How to use a Breadboard
Voltages Drops
How to read and use a Multimeter
Introduction to Arduino with TinkerCAD
What you'll need:
1 - 9 Volt Battery
1 - 9 Volt Battery Harness
1 - Breadboard
1 - Arduino UNO
1 - 100K-Ohm Potentiometer
1 - 470 Ohm Resistor
1 - 5mm Red LED
1 - Jumper Wire Set
In this project, we'll use a flex sensor to control the brightness of an LED. When the flex sensor is bent, the LED's brightness will change accordingly. This project will explore how Arduino reads analog sensors through the voltage drops the sensor creates.
Steps
Set Up the Circuit: Begin by connecting the potentiometer and LED to the Arduino just like you did above:
Connect one end of the flex sensor to the 5V pin on the Arduino.
Connect the other end of the flex sensor to the A0 (analog) pin.
Connect a resistor (around 10k Ohms) from the A0 pin to the GND pin. This creates a voltage divider.
Insert the LED into the breadboard. The longer leg is the positive (anode) and the shorter leg is the negative (cathode).
Connect the cathode of the LED (shorter leg) to a resistor (220-470 Ohms).
Connect the other end of the resistor ground.
Connect the anode of the LED (longer leg) to pin 3.
2. Find the range of your Flex Sensor
Knowing the range of the flex sensor allows you to effectively map its readings to a specific range of values that suits your application. This is crucial for accurate and responsive control using the flex sensor in your projects. Keep in mind that the range may vary depending on the specific flex sensor model and its specifications. It's always a good practice to consult the datasheet or product documentation for accurate information. If you do not have a datasheet, you can also just make sure the first thing you do when making a project with a flex sensor is to test the range using your serial monitor.
The range of the flex sensor is the difference between the highest and lowest readings you recorded. This range will vary depending on the resistor in series with the flex sensor, changing the voltage drop that is read by the Arduino. For example, if the lowest reading is 300 and the highest reading is 900, the range is 600 (900 - 300).
The Arduino has a set of pins labeled with "A" (e.g., A0, A1, A2, etc.) that are specifically designed for reading analog voltages. These pins can read a voltage between 0V (ground) and the reference voltage (usually 5V for most Arduinos).
The reference voltage is the maximum voltage that the ADC can measure. In most cases, it's set to 5V. This means the ADC can represent voltages from 0V to 5V in a range of digital values from 0 to 1023.
The value between 0 and 1023 that the Arduino outputs as an analog signal is derived from the voltage supplied to the input compared to a reference voltage. This voltage drop can be tweaked by altering your voltage drop resistor. Using the Flex sensor in TinkerCAD and a 10K resistor will give you a range from about 256 to 59.
3. Write the Arduino Sketch
In the Arduino IDE, this code would be much shorter and simpler because you can increment pin outputs- in TinkerCAD you cannot so your only option is to do Logic statements. This works fine but is cumbersome. For a sample of this less cumbersome code, check out the AnalogWriteMga code under Analog in the example library on Arduino Create.
int Flex = 0;
int FlexBend = 0;
void setup()
{
pinMode(A0, INPUT);
Serial.begin(9600);
pinMode(3, OUTPUT);
}
void loop()
{
Flex = constrain(analogRead(A0), 0, 255);
Serial.println(Flex);
analogWrite(3, Flex);
delay(10); // Delay a little bit to improve simulation performance
}
4. Understand the Code
pinMode(A0, INPUT); sets the flex sensor as an input.
pinMode(ledPin, OUTPUT); sets the LED pin as an output.
In the loop():
Flex = constrain(analogRead(A0), 0, 255); Constrains the flex sensors range that we just found of 59 to 256 to 0 to 255. This means it will never exceed this range when outputting later in the code. In this case, this is important because we are going to use the flex sensor to control the brightness of an LED which is 0 to 255. A number higher than 255 will turn an LED off.
Serial.println(Flex); Shows us our new constrained values on the serial monitor for reference.
analogWrite(3, Flex); outputs the LED to the flex sensor ranges.
5. Upload and Monitor
Upload the sketch to the Arduino board. Open the Serial Monitor in the Arduino IDE (Ctrl+Shift+M or Tools > Serial Monitor).
6. Experiment and Observe
Bend the Flex Sensor. The LED should brighten and dim as the Flex Sensor is manipulated.
This project demonstrates how to use a flex sensor as an input device to control the brightness of an LED. This concept can be extended to various applications, such as creating interactive lighting or responsive devices based on physical manipulation.
Questions:
How is the flex sensor connected to the Arduino board?
Can you explain the concept of voltage drop across the flex sensor as it bends?
How does the Arduino read the voltage drop across the flex sensor?
Can you think of applications where sensing physical deformation is valuable?
How does this project build on your knowledge of basic Arduino programming and sensor integration?