Kitronik Inventors Kit for Arduino - Exp 3 Dim An LED Using A Potentiometer

Experiment 3 from the Inventors Kit for Arduino. In which we explore dimming an LED using a potentiometer. Included in this resource are code downloads, a description of the experiment, and also a video walkthrough. It provides additional help and is not meant to replace the documentation that ships with the kit. Arduino Inventors Kit Exp 3 Dim An LED Using A Potentiometer Arduino is an open-source code-able electronics platform. The boards can process inputs from many sensors, and also control outputs such as LEDs and motors. The Arduino is controlled by the code with which it is programmed. This code is written in the Arduino programming language, using the Integrated Development Environment (IDE). Once complete, the code is easily transferred to the board using a simple USB lead.

 

Exp 3 Dimming An LED Using A Potentiometer:

This experiment uses a variable to track whether an LED should be turned on or off when a push switch is pressed. An interrupt routine is used to capture the button presses. In this experiment, the main loop is constantly checking and adjusting the brightness of the LED based on the value of a potentiometer – it does this using Pulse Width Modulation (PWM) output. The aims of this experiment are:
  • To use a push switch to turn an external LED ‘on’.
  • Use a potentiometer as a potential divider.
  • To read the analog voltage from the potentiometer and then use it to set the brightness of the external LED.
  • Also, learn how to use software interrupts.
 

Video Walkthrough:

 

Exp 3 Code:

Either open a new Sketch then create the following code by typing in the editor window, or, copy and paste it straight into the editor

/*
  Kitronik Inventors Kit Exp 3
  Switch on and off an LED via an interrupt, and change its brightness in relation to a variable reisitor input

A switch is connected to +5 and Pin 2. 
This triggers an interrupt on the falling edge (i.e. when it has been pressed and released).
The Interrupt toggles a variable which is used to turn on or off the PWN output on Pin 3.
Because this is used in both the main loop and the interrupt it is declared 'volatile'

The LED brightness is controlled by PWM on pin 3. The variable resistor is read into Analog input A0,
and its value is transfered to the PWN output.

  */


const int SwitchInputPin = 2; //Define a human readable name for the input the switch is connected to.
const int LEDDrivePin = 3; // the pin we will use to drive the LED

//Variables used in interrupts are declared volatile, as they may be changed since you last looked at them
volatile byte ButtonState = LOW; //This variable tracks the button clicks.

void setup() 
{
  pinMode(LEDDrivePin, OUTPUT);
  pinMode(SwitchInputPin, INPUT);
  attachInterrupt(digitalPinToInterrupt(SwitchInputPin), OnButtonPressed, FALLING);
}

// This is the interrupt routine that is attached to pin 2 and triggers when the button is released.
void OnButtonPressed() 
{
  ButtonState = !ButtonState;
}


void loop() 
{
  int PotValue = analogRead(A0);  //This variable reads the voltage that the potentiometer is adjusted to
  //Now decide what to do.
  if(ButtonState) //Then we want to turn on the LED
  {
    PotValue = map(PotValue, 0, 1023, 0, 255);  //Due to the ADC and PWM work on different scales, it is required to map the ADC to the same as the PWM
    analogWrite(LEDDrivePin,PotValue);   // turn the LED on to the value of the potentiometer
  }
  else
  {
    digitalWrite(LEDDrivePin, LOW);    // turn the LED off
  }
  
}

Inventors Kit Extra Resources:

Each of the ten experiments has been designed to ease you into coding and also physical computing for the Arduino. The experiments have been chosen to cover the key concepts of physical computing and they also increase in difficulty as you progress. If you are new to this, even the least complex examples can be quite challenging. With this in mind, we created walkthrough videos for each of the experiments. Our presenter talks you through the circuit in a way that backs up the information given in the booklet but also in a style that some might find easier to absorb. As well as having a video walkthrough, each page also contains the code. Although it is always good to tackle the code yourself, it can be handy for testing your circuit. The code has been heavily commented as an extra learning resource. To get the most out of the experiment, once you've tested your circuit have a go at coding the experiment from scratch. Follow the links in the table below:  
Exp No#. Experiment Name.
1
Digital Inputs & Outputs.
2
Light Sensor & Analog Inputs.
3
Dimming an LED using a potentiometer.
4
Using a transistor to drive a motor.
5
Control a servo with a potentiometer.
6
Setting the tone with a piezo buzzer.
7
Using a seven segment display.
8
Exploring wind power.
9
Capacitor charge circuit.
10
Using an RGB LED.

Leave a comment

All comments are moderated before being published