You are on page 1of 24

ECE 2030

Arduino Based System


Design

Daw Khaing Su Wai


Experiment 8

Circuits with Potentiometers


Potentiometers
• A potentiometer referred to as a pot that provides a variable resistance, which
we can read into the Arduino board as an analog value.
• In this example, that value controls the rate at which an LED blinks.
• Potentiometers have a range of resistance.
• They can be attuned from zero ohms to whatever maximum resistance that is
specific to it.
• For example, a potentiometer of 10 kΩ can be adjusted from 0 Ω to its maximum
of 10 kΩ.
Potentiometers
Potentiometers
• All potentiometers have three pins.
• The first goes to ground from one of the outer pins of the potentiometer.
• The second goes from 5 volts to the other outer pin of the potentiometer.
• The third goes from analog input 2 to the middle pin of the potentiometer.
• This changes the relative "closeness" of that pin to 5 volts and ground, giving us a
different analog input.
• When the shaft is turned all the way in one direction, there are 0 volts going to
the pin, and we read 0.
• When the shaft is turned all the way in the other direction, there are 5 volts going
to the pin and we read 1023.
• In between, analogRead() returns a number between 0 and 1023 that is
proportional to the amount of voltage being applied to the pin.
Potentiometers
Analog to Digital Converter
• The signal from the pot is analog, but we want it to be digital so that we can read
the value on the screen.
• Arduino has a built-in ADC (analog-to-digital converter) which creates a digital
representation of the analog signal.
• The higher the input voltage, the larger the digital value. Arduino’s ADC takes in 0
to 5 volts and has a 10-bit resolution which lets it output 1024 digital values.
• To summarize: 0 volts is equivalent to a digital value of 0, and 5 volts is equivalent
to a digital value of 1023.
Circuit Diagram
Reading a Potentiometer and Changing Delay Times
//Global Variables
int sensorValue = 0; //initialization of sensor variable
void setup() {
Serial.begin(9600);
}
void loop(){
sensorValue = analogRead(A0); //read the sensor value using ADC
Serial.println(sensorValue); //print digital value to serial monitor
delay(50); //50ms delay
}
LED Blinking Control by Potentiometer
LED Blinking Control by Potentiometer
const int potPin = 0; // select the input pin for the potentiometer
const int ledPin = 5; // select the pin for the LED
int val = 0; // variable to store the value coming from the sensor
void setup() {
pinMode(ledPin, OUTPUT); // declare the ledPin as an OUTPUT
Serial.begin(9600);
}
void loop() {
val = analogRead(potPin); // read the voltage on the pot
digitalWrite(ledPin, HIGH); // turn the ledPin on
delay(val); // blink rate set by pot value (in milliseconds)
digitalWrite(ledPin, LOW); // turn the ledPin off
delay(val); // turn led off for same period as it was turned on
Serial.println(val);
}
Converting LED Brightness by Potentiometer
const int kPinPot = A0;
const int kPinLed = 9;
void setup() {
pinMode(kPinLed, OUTPUT);
Serial.begin(9600);
}
void loop() {
int ledBrightness;
int sensorValue = 0;
sensorValue = analogRead(kPinPot);
ledBrightness = map(sensorValue, 0, 1023, 0, 255);
Serial.println(ledBrightness);
analogWrite(kPinLed, ledBrightness);
}
Changing Potentiometer Reading to a Percentage
/*
Potentiometers : Changing Potentiometer Reading to a Percentage
*/
int potPin = A0; // potentiometer is connected to analog 0 pin
int potValue; // variable used to store the value coming from the sensor
int percent; // variable used to store the percentage value
void setup() {
Serial.begin(9600); // initialize the serial communication
// Note: analog pins are automatically set as inputs
}
void loop() {
potValue = analogRead(potPin);
// get a reading from the potentiometer, assign the name potValue
percent = map(potValue, 0, 1023, 0, 100); // convert reading to a percentage
Changing Potentiometer Reading to a Percentage
Serial.print("Analog Reading: "); // print out the potentiometer reading
Serial.print(potValue);
Serial.print(", Percentage: "); // print out the percentage
Serial.print(percent);
Serial.println("%");
delay(1000); // wait a second
}
Running LED controlled by a potentiometer
#define pinNumPot 0 // pot is to be connected to the Analog pin no. 0
#define FALSE 0
#define TRUE 1
const int LedPins[] = {2, 3, 4, 5, 6, 7};
const int NumLEDs = 6;
const int perLEDdelay = 150; // in milliseconds
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
for(int led = 0; led < NumLEDs; led++)
{
pinMode(LedPins[led], OUTPUT); // Set the LEDs in OUTPUT mode
}
}
Running LED controlled by a potentiometer
void loop() {
// put your main code here, to run repeatedly:
int blinkRate = perLEDdelay;
int potValue = 0;

potValue = analogRead(pinNumPot); // returns a value between 0 and 1023


Serial.println(potValue);

blinkRate = blinkRate - (potValue/10);


// The rate of running LED is decreased as we get higher value from the sensor (pot)

runningLED(blinkRate);
} // end of loop()
Running LED controlled by a potentiometer
void runningLED(int rate)
{
for(int i = 0; i < NumLEDs; i++)
{
digitalWrite(LedPins[i], HIGH); // switch ON the LED
delay(rate);
digitalWrite(LedPins[i], LOW); // switch OFF the same LED
}
} // end of runningLED()
Exercises
1. Construct an Arduino program that controls six LEDs using the potentiometer such that ON,
OFF of these LEDs can be changed based on the running LEDs controlled by a potentiometer.
When the potentiometer is rotated in the clockwise direction the six LEDs turn ON series of
LEDs and when the potentiometer is rotated anti-clockwise the LEDs turn OFF series of LEDs.
Experiment 9

Controlling RGB LEDs


Controlling RGB LEDs
• The RGB led consists of three different led’s. These led’s are red, green and blue and can obtain
many other colors by mixing up these colors.
• The Arduino has a analog write function which obtaining different colors for Arduino RGB led.
• There are actually two types of RGB led’s; the common cathode one and the common anode one.
• In the common cathode RGB led, the cathode of all the led’s is common and give PWM signals to
the anode of led’s.
• In the common anode RGB led, the anode of all the led’s is common and give PWM signals to the
cathode of led’s.
• You cannot distinguish between the common cathode and common anode type by just looking at
the RGB led because both look same. You will have to make the connections to see that either it is
common cathode or common anode.
• The RGB led has one big lead than the other leads. In the common cathode case, it will be
connected to GND and in the common anode case; it will be connected to 5V.
Controlling RGB LEDs
Controlling RGB LEDs
• If you are using the common anode RGB led, connect the cathode of the RGB led which is the
longer pin of RGB led to the GND of Arduino and the other three pins to the pin 11, 10, 9 of
Arduino through the 220 ohm resistors.
• The resistors will prevent the excess amount of current to flow through the RGB led.
• If you are using the common anode RGB led, then connect the long lead to the 5V of Arduino.
• Note
• If you have any other Arduino, then make sure that you are using the PWM pins of that Arduino.
The PWM pins have a ~ sign with them.
• Inside the RGB led, there are three more led’s.
• So by changing the brightness of these led’s, can obtain many other colors.
• To change brightness of RGB led, can use the PWM pins of Arduino.
• The PWM pins will give signal different duty cycles to the RGB led to obtain different colors.
Circuit Diagram
Code
int red_light_pin= 11;
int green_light_pin = 10;
int blue_light_pin = 9;
void setup() {
pinMode(red_light_pin, OUTPUT);
pinMode(green_light_pin, OUTPUT);
pinMode(blue_light_pin, OUTPUT);
}
void loop() {
RGB_color(255, 0, 0); // Red
delay(1000);
RGB_color(0, 255, 0); // Green
delay(1000);
RGB_color(0, 0, 255); // Blue
delay(1000);
RGB_color(255, 255, 125); // Raspberry
delay(1000);
Code
RGB_color(0, 255, 255); // Cyan
delay(1000);
RGB_color(255, 0, 255); // Magenta
delay(1000);
RGB_color(255, 255, 0); // Yellow
delay(1000);
RGB_color(255, 255, 255); // White
delay(1000);
}
void RGB_color(int red_light_value, int green_light_value, int blue_light_value)
{
analogWrite(red_light_pin, red_light_value);
analogWrite(green_light_pin, green_light_value);
analogWrite(blue_light_pin, blue_light_value);
}

You might also like