Inthisprojectyou'lllearnhowtocontrolthebrightnessofanledusinganyremote
STEP1:Whatyou'llneed
Forthistutorialyou'llneed:
- Arduinouno
- 221 Ohm resistor
- Jumper wires
- IR Receiver & Remote
- Breadboard
- LEDs
STEP2:TheCircuit
If you don't know how IR Receivers and Transmitters work you can click this link or this to learn more on them
The pin layout on most breakout boards look like this:
The pin layout of most stand-alone diodes is like this:
Source of images: https://www.circuitbasics.com/arduino-ir-remote-receiver-tutorial/
STEP 3: The Code
How it works
Before we look at the code we need to first know the button layout of our remote. First you have to download the IRremote library from here To install the library from the ZIP file, open up the Arduino IDE, then go to Sketch > Include Library > Add.ZIP Library, then select the IRremote ZIP file that you downloaded When you're done Upload the code below and open the serial monitor (This is also a great opportunity to see if your pin layout is correct)
#include <IRremote.h>
const int RECV_PIN = 8;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup(){
Serial.begin(9600);
irrecv.enableIRIn();
irrecv.blink13(true);
}
void loop(){
if (irrecv.decode(&results)){
Serial.println(results.value, HEX);
irrecv.resume();
}
}
If
nothing
seems
to
be
happening
(LED on Arduino doesn't flash when you press a button) check your pin layout you might have swapped Vcc with S
You can map out each button of your remote and save it in a txt file for reference later.
NOTE: When you hold down a button continuously the serial monitor would print 0XFFFFFFFF
Now that you know the button layout of your remote lets look at the code bit by bit
#include <IRremote.h>
const int RECV_PIN = 7;
IRrecv irrecv(RECV_PIN);
decode_results results;
const int ledPin = 10;
int brightness=0;
unsigned long key_value = 0;
To use the IRremote library, we need to create an object called irrecv
and specify the pin number where the IR receiver is connected (line 3). This object takes care of the protocol and processing of the information from the receiver.
The next step we create an object called results
from the decode_results
class, which will be used by the irrecv
object to share the decoded information with our application (line 5).
Next we create variables to Set up pin 10 for our LED and to store the value of our brightness the last variable would be explained later
void setup(){
irrecv.enableIRIn();
irrecv.blink13(true);
pinMode(ledPin, OUTPUT);
}
In the void setup()
block, first we set up the serial monitor baud rate. Next we start the IR receiver by calling the IRrecv
member function enableIRIn()
(line 10).
The irrecv.blink13(true)
function on line 11 will blink the Arduino’s on board LED every time the receiver gets a signal from the remote control, which is useful for debugging.
The pinMode(ledPin, OUTPUT) function Configures ledPin (pin 10) to behave as an output
void loop() {
if (irrecv.decode(&results)){
.
.
.
.
.
.
}
}
In the void loop()
block, the function irrecv.decode
will return true if a code is received and the program will execute the code in the if statement. The received code is stored in results.value
.
if (results.value == 0XFFFFFFFF)
results.value = key_value;
.
.
.
.
.
key_value = results.value;
In the block above If we receive 0XFFFFFFFF from the remote, it means a repetition of the previous key. So in order to handle the repeat key pattern, we store the hex code in a global variable key_value
every time a code is received:
key_value = results.value;
When you receive a repeat pattern, then the previously stored value is used as the current key press.
if(results.value==0xF076C13B){
brightness-=5;
if (brightness<0){brightness=0;}
}
else if(results.value==0xA3C8EDDB){
brightness+=5;
if (brightness>255){brightness=255;}
}
The
if
condition
checks
if
the
button
pressed
is
a
+
or
-
(your HEX code might be different depends on your remote and receiver) and increases or decreases the value of the brightness. The next condition constrains the value of brightness between 0 & 255
irrecv.resume();
irrecv.resume()
is
used
to reset the receiver and prepare it to receive the next code.
brightnessControl(brightness);
This line calls the brightnessControl() function and passes in the current value of our brightness as its argument.
void brightnessControl(int value){
analogWrite(ledPin,value);
}
The brightnessControl() method takes an integer as its argument and set the brightness of our led using PWM
For more info on PWM click here or here
For more info on IRremote click here
STEP 4: Congratulations!!!You've successfully finished this project. Well done!
I hope you liked this, If you have any suggestions on how to make my code, let me know in the comments.
THANK YOU
Comments
Please log in or sign up to comment.