Lesson 4 Controlling an LED by Potentiometer

Share for us

Introduction

In the last experiment, you have learned how to control an LED by PWM programming, which is interesting though sounds slightly abstract. In this lesson, you will learn how to change the luminance of an LED by potentiometer.

Components

– 1 * SunFounder Uno board

– 1 * Breadboard

– Jumper wires

– 1 * Resistor (220Ω)

– 1 * LED

– 1 * Potentiometer

– 1 * USB cable

Principle

Analog V.S. Digital

A linear potentiometer is an analog electronic component. So what’s the difference between an analog value and a digital one? Simply put, digital means on/off, high/low level with just two states, i.e. either 0 or 1. But the data state of analog signals is linear, for example, from 1 to 1000; the signal value changes over time instead of indicating an exact number. Analog signals include those of light intensity, humidity, temperature, and so on.

In this experiment, a potentiometer, or pot, is used to change the current in the circuit so the luminance of the LED will change accordingly. And since the pot is an analog device, the current change is smooth, thus the LED will gradually get brighter or dimmer instead of going through an obvious stepwise process.

What we mean by PWM here is the digitalization of analog signals, which is a process of approaching analog signals. Since the potentiometer inputs analog signals, it should be connected to analog ports, i.e. A0-A5, instead of digital ports.

Experimental Procedures

Step 1: Build the circuit

The schematic diagram

As you see, the potentiometer is connected to pin A0 of the SunFounder Uno board, which can measure voltages from 0V to 5V. The corresponding returned value is from 0 to 1024. The measurement accuracy for voltage change is relatively high.

Step 2: Program (please go to our official website www.sunfounder.com to download related code by clicking LEARN -> Get Tutorials)

Step 3: Compile the code

Step 4: Upload the sketch to the SunFounder Uno board

Rotate the shaft of the potentiometer and you should see the luminance of the LED change.

Code 

//Controlling led by potentiometer//Rotate the shaft of the potentiometer and you should see the luminance of the LED change.
//Email:support@sunfounder.com
//Website:www.sunfounder.com
//2015.5.7
/******************************************/
const int analogPin = A0;//the analog input pin attach to analog pin A0
const int ledPin = 9;//the led attach to pin 9
int inputValue = 0;//variable to store the value coming from sensor
int outputValue = 0;//variable to store the output value
/******************************************/
void setup()
{
}
/******************************************/
void loop()
{
inputValue = analogRead(analogPin);//read the value from the sensor
outputValue = map(inputValue,0,1023,0,255);//Convert from 0-1023 proportional to the number of a number of from 0 to 255
analogWrite(ledPin,outputValue);//turn the led on depend on the output value
}
/*******************************************/ 

Video