Analog Arduino Projects with TinkerCAD

Exercise One: Analog Mapping

Background Knowledge:

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

 2. Write the Arduino Sketch

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

In the loop():

4. Upload and Monitor

 5. Experiment and Observe

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: 

Exercise Two: Variable Blinking

Background Knowledge:

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

 2. Write the Arduino Sketch

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

In the loop():

4. Upload and Monitor

 5. Experiment and Observe

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: 

Exercise Three: LED Fading Scale

Background Knowledge:

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

 2. Write the Arduino Sketch

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

In the loop():

4. Upload and Monitor

 5. Experiment and Observe

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: 

Exercise Four: Flex Sensor (Understanding Voltage Drops)

Background Knowledge:

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

 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. 

 3. Write the Arduino Sketch

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

In the loop():

 5. Upload and Monitor

6. Experiment and Observe

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: