Potentiometer or variable resistor control LED Code

The if() statement is the most basic of all programming control structures. It allows you to make something happen or not depending on whether a given condition is true or not. It looks like this:

if (someCondition) {
// do stuff if the condition is true
}

There is a common variation called if-else that looks like this:

if (someCondition) {
// do stuff if the condition is true
} else {
// do stuff if the condition is false
}

There’s also the else-if, where you can check a second condition if the first is false:

if (someCondition) {
// do stuff if the condition is true
} else if (anotherCondition) {
// do stuff only if the first condition is false
// and the second condition is true
}

You’ll use if statements all the time. The example below turns on an LED on pin 13 (the built-in LED on many Arduino boards) if the value read on an analog input goes above a certain threshold.

Circuit

image developed using Fritzing. For more circuit examples, see the Fritzing project page

Schematic:

Potentiometer or variable resistor control LED Code schematic

Code

In the code below, a variable called analogValue is used to store the data collected from a potentiometer connected to the Arduino on analogPin 0. This data is then compared to a threshold value. If the analog value is found to be above the set threshold the LED connected to digital pin 13 is turned on. If analogValue is found to be < threshold, the LED remains off.

[box color=”#985D00″ bg=”#FFF8CB” font=”verdana” fontsize=”14 ” radius=”20 ” border=”#985D12″ float=”right” head=”Major Components in Project” headbg=”#FFEB70″ headcolor=”#985D00″]

Hardware Required

  • Arduino Board
  • (1) Potentiometer or variable resistor
  • (1) 220 ohm resistor
  • (1) LED
  • hook-up wire

[/box]

For more detail: Potentiometer or variable resistor control LED Code


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top