Potentiometer Experiments
After completing the Potentiometer project, test yourself by trying these experiments.
1
| Make a light dimmer. Use the potentiometer to adjust the brightness of an external LED connected on pin 3.
|
|
2
| You may have noticed the LED cycles through between off and full bright four times when you rotate the potentiometer from one end to the other end.
This is because the minimum and maximum values for the analogRead command is 0 and 1023, whereas,
the minimum and maximum values for the analogWrite command is 0 and 255.
To correct the problem, we need to map the input range (0 to 1023) to the output range (0 to 255)
using the map command.
|
value = analogRead(potentiometerPin);
int newValue = map(value, 0, 1023, 0, 255);
analogWrite(led, newValue);
Serial.print(value);
Serial.print(" ");
Serial.println(newValue);
|
3
| Use the potentiometer to adjust the blinking speed of an external LED on pin 3.
|
value = analogRead(potentiometerPin);
digitalWrite(led, HIGH);
delay(value);
digitalWrite(led, LOW);
delay(value);
|
4
| Use the potentiometer to adjust the tone frequency of the buzzer connected to pin 8.
Hint: You'll need to use the map command to change the input range (0 to 1023) to an audible frequency range such as from 262 to 1047.
|
value = analogRead(potentiometerPin);
int frequency = map(value, 0, 1023, 262, 1047);
tone(buzzer, frequency);
|
5
| Connect up five (5) external LEDs.
Use the potentiometer to adjust the speed for the five LEDs to cycle through.
|
|
6
| Connect up the RGB LED. Use the potentiometer to change the color on the RGB LED.
- Turn on only the red LED if the value from the potentiometer is less than 300.
- Turn on only the green LED if the value from the potentiometer is between 301 and 600.
- Turn on only the blue LED if the value from the potentiometer is between 601 and 1023.
Hint: you'll need to use the if command like this...
|
if (value < 300) {
// code to turn on the red LED
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
} else if (value <= 600) {
// insert the code to turn on the green LED
} else if (value <= 1023) {
// insert the code to turn on the blue LED
}
|
7
| Connect up the 7-segment LED display. Use the potentiometer to dial a number to show on the display.
- Show the number 0 if the value from the potentiometer is between 0 and 9.
- Show the number 1 if the value from the potentiometer is between 10 and 19.
- Show the number 2 if the value from the potentiometer is between 20 and 29.
- etc.
Hint: you'll need to use the if command like this...
|
if (value >=0 && value <= 9) {
// code to display the number 0
displayDigit(0);
} else if (value >= 10 && value <= 19) {
// code to display the number 1
displayDigit(1);
} else if ...
// add the correct code
}
|
|