Object - Lab 3
Overview
This lab explores analog input and output using variable resistors. You will learn to read sensor values to the serial monitor and utilize pulse width modulation (PWM) and frequency modulation to affect outputs.
Objectives:
- Read Analog sensor values using analogRead()
- Print sensor values to the serial monitor
- Use analogWrite90 on a PWM pin to dim an LED
- Learn to fit an analog input reading into a single byte using the map() function
- Use frequency modulation with the tone() command to to generate sounds
Part 1: Potentiometers
In part 1, we were tasked with building a circuit with a potentiometer as the input, and an LED as the output. While utilizing analogRead to bring in the values of the potentiometer and analogWrite to convert the values for the brightness of the LED. However, bringing together schemantics of the hardwired components and adding code to those specific inputs and outputs is really a great way to start understanding more complex ideas.
Schemantics
The schemantic below is a diagram that is utilizing one potentiometer from analog input pin A0 that is going straight to ground while we have a LED from digital output pin 9 with a PWM. A 220 ohm resistor is needed to keep the LED path of energy having more resistance in order to limit the current to a safe value. When using the potentiometer it is necessary to pull down to ground to complete the circuit.
Code
The code below is a basic guideline for creating a connection between the potentiometer and LED. First we have to declare are global variables that will be needed when defining elements in both setup and loop. LED is being set to value 9 while POT (potentiometer) is being set to analog value A0. By setting up these values in the beginning will allow us to call the values in specific functions.
In void setup(), we need to initialize serial. By initializing the serial, we are setting the baud rate. The baud rate specifies how fast data is sent over a serial line. We will be using 9600 since speed is not critical in this assignment. As well as, creating an pinMode() to direct that our LED in pin 9 will be the OUTPUT for data brought in by the pot input.
// declare a global constant
// give the LED pin numbers a name
int LEDpin = 9;
int brightness = 0;
// declare an int to hold the pot value
int pot = A0;
void setup() {
// put your setup code here, to run once:
// initialize serial
Serial.begin(9600);
// set led pin as an output
pinMode(LEDpin, OUTPUT);
}
void loop() {
int potVal = analogRead(pot);
// declare int for brightness value
brightness = map(potVal, 0, 1023, 0, 255);
// used for writing PWM to LED
analogWrite(LEDpin, brightness);
Serial.print("Pot Value: ");
Serial.println(potVal);
}
Photo and video of working circuit and code
Part 2: Other Variable Resistors
In part 2, we had to build a circuit with at least 2 different variable resistors that output brightness values to two different LEDs. For the build we need two voltage divider circuits that connect to A0 and A1, using variable resistors for R3 and R4. To both pin 9 and 10, we need to connect a resistor + LED.
Schemantics
The schemantic below is a diagram that is utilizing two sensor resistors (photocell and pressure sensor) connected to analog input pin A0 and A1 that is going straight to ground while we have a two LED to digital output pin 5 and 6 with a PWM. A 220 ohm resistor is needed to keep the LED path of energy having more resistance in order to limit the current to a safe value.
Code
The code for part 2 is similar to that of the part above. The code below is a basic guideline for creating a connection between the two different sensors and two LED. First we have to declare are global variables that will be needed when defining elements in both setup and loop. LEDs are being set to values 5 and 6 while sensors (force and photocell) is being set to analog value A0 and A1. By setting up these values in the beginning will allow us to call the values in specific functions.
A photocell changes resistance depending on the amount of light it is exposed to. These little sensors make great ambient light triggers (when light in the room turns on, do something). For this lab, it will act as a useful input for creating a dim on an LED when effected by the amount of light.
A pressure sensor is based on the harder you press, the lower the sensor’s resistance. Resistance changes only when pressure is applied to the round area at the end of the sensor.
This sensors are based on resistance allowing us to use them to create a flow of energy (current) that will respond and output that response visually through the LED.
// declare a global constant
// give the LED pin numbers a name
int LEDpin1 = 5;
int LEDpin2 = 6;
int force = A1;
int photo = A0;
void setup() {
// put your setup code here, to run once:
// initialize serial
Serial.begin(9600);
// set led pin as an output
pinMode(LEDpin1, OUTPUT);
pinMode(LEDpin2, OUTPUT);
}
void loop() {
int forceVal = analogRead(force);
int photoVal = analogRead(photo);
// declare int for brightness value
int brightness1 = map(forceVal, 0, 1023, 0, 255);
int brightness2 = map(photoVal, 0, 200, 0, 255);
// used for writing PWM to LED
analogWrite(LEDpin1, brightness1);
analogWrite(LEDpin2, brightness2);
Serial.print("Force, Photo Value: ");
Serial.println(forceVal);
Serial.println(photoVal);
}
Photo and video of the final working product
Part 3: Tone Output
In part 3, we are tasked with connecting two photoresistors to analog pin 0 in a voltage divider circuit as shown below. The 8-ohm speaker connects to pin 8 of the Arduino and the other end of the speaker connects to ground.
Schemantics
The schemantic below is a diagram that is utilizing two photocell sensors from the same analog input pin A0. However, one is being used as a resistor to the latter that will be input values to create a current that will not be over powering. However, one photocell would not be enough to power the range for creating different sound frequencies to the microphone. Different from part 1 and 2, we will be using a speaker as the output that is being set to pin 8.
Code
Similar to part 1 and 2, we have to define our global variables. Now with one output speaker at pin 8 and one analog input at analog pin A0 (but this will be in a path with two photocell senors). These will help us create different sound frequencies outputted by the speaker. Again, we have set the serial at 9600 to communicate to the sensors and outputs in a constant rate. Then we will set the speaker to int micro with pinMode to 9.
In void loop(), we will remap to different values since the speaker is at a different rate (100 - 1000). By mapping to these values, we are able to create different frequencies in a standard frequency note. Then we will use a new built in function tone() that takes three perimeters output (micro), freq (remapped photocell input to frequency range), and the duration of the sound to create an effect of changing frequencies when the photocell gets more light and less light.
// declare int variable to store sensorReading
int micro = 8;
int photo = A0;
// declare float variable for our frequency value
float freq;
void setup() {
// initialize serial communications
Serial.begin(9600);
pinMode(micro, OUTPUT);
}
void loop(){
// read the analog input on A0, set sensorReading to this value
int photoVal = analogRead(photo);
// map sensorReading to a an output between 100 and 1000
freq = map(photoVal, 0, 1023, 100, 1000);
// print it to the serial monitor
analogWrite(micro, freq);
//change the pitch, play for 10ms - use tone()
tone(micro, freq,10);
}
Photo and video of the final working product