Adjust the voltage

In this lesson, help Ohmie adjust the voltage controlled with the potentiometer.

A potentiometer (also called a pot) is a variable resistor that change the resistance from 0 ohms to a certain maximum value based on the type. Using a pot, you can easily adjust the voltage up and down and the Arduino board can measure it in real time. Let's try it!

Step 1 - Add the potentiometer

Add the potentiometer to the breadboard. Simply insert it across the middle section of the breadboard.

In your kit, the value of the potentiometer is 10K ohms or 10,000 ohms.

Step 2 - Add the analog wire

Add a yellow wire to your circuit from the middle of the potentiometer to the Analog Input A0 (top pin of the the lower header on the left).

The analog input (A0 pin) is will be measuring the voltage continuously. When you adjust the pot, the voltage will increase and decrease. The Arduino will keep capturing the value and you will have access to it in your code.

Step 3 - Connect the potentiometer

Connect the power to the potentiometer.

Add a red wire from the first pin of the pot to the 5V power supply (the right bottom header). Remember to use a red wire for the power source.

Add a black wire from the last pin available on the potentiometer to the ground (GND).

This particular setup creates a voltage divider - this is something we will learn in a later edition of the PodPi magazine.

Step 4 - Add an LED too

Let's add an LED to see if we can control its behavior with the information received from the potentiometer.

Add a resistor on digital pin 11 and to the breadboard. Add an LED on the same row as the resistor (with the anode +) and the other side of the LED (cathode -) to another row.

Add a black or blue wire from the cathode to the ground header (GND) below.

Your circuit is all set, let's code!

The code

// Load the johnny-five library and commandsvarfive=require("johnny-five");
varboard=newfive.Board();
// Execute this function when the board is readyboard.on("ready", function() {
varpot=newfive.Sensor('A0'); // new sensor on Analog Pin A0varled=newfive.Led(11); // new LED on Digital Pin 11varvalue=0; // record a value// Execute this function each time the pot variable changespot.on('change', function() {
value=this.value; // record the current valueconsole.log('Potentiometer value is: '+value); // send messageled.brightness(Math.abs((value/4))); // set the brightness to 1/4 of the value
});
});

Experiment #1 - Change the blink rate

You can change the blink rate of the LED by using the value returned by the potentiometer and pass it as the blink value inthe blink call. Check out the code below:

// Load the johnny-five library and commandsvarfive=require("johnny-five");
varboard=newfive.Board();
varclicks=0; // variable to record the number of clicks// Execute this function when the board is readyboard.on("ready", function() {
varpot=newfive.Sensor('A0'); // sensor on Analog Pin A0varled=newfive.Led(11); // LED on Digital Pin 11varvalue=0; // variable to record a value// Execute this function each time the sensor reads a new valuepot.on('change', function() {
value=this.value; // record the valueconsole.log('Potentiometer value is: '+value); // send messageled.blink(value); // blink the LED using the value as the time
});
});