Buy from US Buy from UK Buy from DE Buy from IT Buy from FR Buy from ES Buy from JP

Content

  1. Introduction
  2. Preparations
  3. About Potentiometer
  4. Connection
  5. Upload Sketch
  6. Running Result

Introduction

In this lesson, we will use a potentiometer module to control the light brightness of LED module. Both modules are connected to Osoyoo Magic I/O board for Arduino.

Preparations

Hardware

Software

Arduino IDE (version 1.6.4+)

About Potentiometer

In this lesson, a potentiometer, or pot, is used to change the PWM current in the circuit so the brightness of the LED will change accordingly. And since the pot is an analog device, the current change is smooth, thus the blink brightness will gradually get bigger or smaller instead of going through an obvious stepwise process.

So what’s the difference between an analog value and a digital one? Simply put, digital means on/off, high/low voltage with just two states, i.e. either 0 or 1. But the data state of analog signals is a continuous range, for example, from 1 to 1023;  Analog signals include those of light intensity, humidity, temperature, and so on.

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.

Electronic symbol
Potentiometer symbol Europe.svg (International)
Potentiometer symbol.svg (US/Canada)

Connection

First, please plug Osoyoo Magic I/O shield into UNO board:

Then connect the Potentiometer module to the A9 port of the Magic I/O shield with a 3-pin PNP cable as below:

Upload Sketch

After above operations are completed, connect the Arduino board to your computer using the USB cable. The green power LED (labelled PWR) should go on.

Code Program

You can download the sketch from this link or copy below code to your Arduino IDE window:

int potPin = A0;    // select the input pin for the potentiometer
int ledPin = 9;   // 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
}

void loop() {
  val = analogRead(potPin);    // read the value from the sensor
  digitalWrite(ledPin, HIGH);  // turn the ledPin on
  delay(val);                  // stop the program for some time
  digitalWrite(ledPin, LOW);   // turn the ledPin off
  delay(val);                  // stop the program for some time
}

By turning the shaft of the potentiometer, we change the amount of resistence on either side of the wiper which is connected to the center 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.

The circuit of this part is same as above.You can get the sketch from this link or copy below code to your new Arduino IDE window and upload it to your Arduino board. Don’t forget to choose the corresponding board and port for you project!

const int analogPin = A0;//the analog input pin attach to
const int ledPin = 9;//the led attach to
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
}

Compile and upload

Open the Arduino IDE and select corresponding board type and port type for your Arduino board.

After compile this sketch, simply click the “Upload” button in the environment. Wait a few seconds – you should see the RX and TX leds on the board flashing. If the upload is successful, the message “Done uploading.” will appear in the status bar.

Running Result

As you see, the potentiometer is connected to pin A0 of the Osoyoo 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.

A few seconds after the upload finishes,rotate the shaft of the potentiometer and you should see the luminance of the LED change.