Menu

A simple project with Arduino

August 9, 2019 - General, C Language, C++
A simple project with Arduino

 
 
 
 
 
 
Hey everyone! Yesterday I posted a picture of the little project I was working on and I never thought it would generate so much interest. I talked a little bit about what I was working on, I was working on a wirless fan controlled by an IR (infrared) remote and I also posted a video of me playing with the IR remote and some LEDs. In less than a day, the post reached 100 likes and people are still asking for the source. They want to know how to to this, so in this article I’m going to show you how to code the wirless fan and put the pieces together but I will assume that you guys have already some basic knowledges of Arduino. I a next article I will start from 0 for the very beginners but for now here is what you will need:

Essentials
1. Arduino UNO Board
2. Breadboard
3. Wires
4. 9V battery
5. Fan
6. IR Receptor
7. 1 220ohms resistors
8. IR Remote

Optionals
1. 3 LEDS
2. 3 220hms resistors
3. Wires

First it can seems like a lot of specific stuff to have but on amazon you can order a complete kit for beginners with all that stuff at a very cheap price: https://www.amazon.com/Smraza-Tutorials-Compatible-Mega2560-Projects/dp/B01L0ZL8N6/ref=sr_1_8?crid=1V6CLG9URI62O&keywords=arduino+uno+kit&qid=1565349235&s=gateway&sprefix=arduino+uno%2Caps%2C167&sr=8-8

To make this project you don’t even need to have the same board I have but if you want to do it almost exactly like I did, you will need a Arduino UNO board:
 

 
Now lets take a look at a simplified schema to give you and idea of what we will be doing.
Note: Always disconnect your board when you are adding and changing things. Also make sur you don’t try to connect + with –.
 

 
Here are the step to create the schema:
– Connect you 9V Battery on the pins VIN and GND at the bottom of your board (Your board is now wireless).
– Connect your IR Receptor on the pins 5V, GND (top of the board), pin number 4 (important).
– Now you just have to connect your fan (don’t forget all the board is now working with 9V except the pins 5V and 3.3V). Connect the + on the pin VIN and the – on a pin of your choice (In my case 10).

When you will have completed these steps you will only have to write the code to make it work. That is the esiest part and I will even give you the source code right there (still a first version so many repetitions):

IMPORTANT: Make sure you downloaded and installed the librairie IRremote else the code won’t compile

#include <IRremote.h>
#include <IRremoteInt.h>

IRrecv irrecv(RECV_PIN);
IRsend irsend;
int var = 0;
int led = 4, led2 = 5, led3 = 6, irLed = 10;
bool lightOn = false, light2On = false, light3On = false, 
intervall = false, turnOnFan = false;
decode_results results;

void init_values(){
  lightOn = false;
  light2On = false;
  light3On = false;
  intervall = false;
  turnOnFan = false;

  pinMode(led, OUTPUT); 
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT); 
  pinMode(irLed, OUTPUT); 
  digitalWrite(led, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(irLed, HIGH);
}

int turn_on(unsigned long on, unsigned long off, bool *output){
    if(results.value == on){
      Serial.println("ON");
      *output = true;
    }
    else if(results.value == off){
      Serial.println("OFF");
      *output = false;
    }
}

void output(){
    if(lightOn){
      digitalWrite(led, LOW); 
    }
    else if(lightOn == false){
      digitalWrite(led, HIGH); 
    }

    if(light2On){
      digitalWrite(led2, LOW); 
    }
    else if(light2On == false){
      digitalWrite(led2, HIGH); 
    }

    if(light3On){
      digitalWrite(led3, LOW); 
    }
    else if(light3On == false){
      digitalWrite(led3, HIGH); 
    }
}

void setup(){
  Serial.begin(9600);  // Start the serial console
  irrecv.enableIRIn(); // Start the receiver
  init_values();
}

// the loop function runs over and over again forever
void loop(){
  if (irrecv.decode(&results)) 
  { 
    var = results.value;
    Serial.print("Result: ");
    Serial.println(results.value, DEC);
    Serial.println(results.value, HEX);

    output();

    if(turnOnFan)
    {
      digitalWrite(irLed, LOW); 
    }
    else if(turnOnFan == false)
    {
      digitalWrite(irLed, HIGH); 
    }

    if(intervall)
    {
      uint32_t start = millis();
      while (millis() - start < 5000){
        if(millis() - start > 1000)
            digitalWrite(led, LOW);
        if(millis() - start > 2000)
            digitalWrite(led2, LOW);
        if(millis() - start > 3000)
            digitalWrite(led3, LOW);
        if(millis() - start > 4000)
        {
            digitalWrite(led, HIGH);
            digitalWrite(led2, HIGH);
            digitalWrite(led3, HIGH);
            start = millis();
        }
      }     
    }

    turn_on(0xFFA25D, 0xFF22DD, &lightOn);
    turn_on(0xFF629D, 0xFF02FD, &light2On);
    turn_on(0xFFE21D, 0xFFC23D, &light3On);

    if(results.value == 16769055)
    {
      Serial.println("ON");
      intervall = true;
    }
    else if(results.value == 0xFF9867)
    {
      Serial.println("OFF");
      intervall  = false;
      lightOn = false;
      light2On = false;
      light3On = false;
      turnOnFan = false;
    }

    if(results.value == 16718055)
    {
      Serial.println("TURN ON");
      turnOnFan = true;
      lightOn = true;
      light2On = true;
      light3On = true;
    }
    else if(results.value == 16730805)
    {
      Serial.println("OFF");
      turnOnFan = false;
      lightOn = false;
      light2On = false;
      light3On = false; 
    }

    irrecv.resume(); // Receive the next value
  }
}

 
Finally, it should work, if not maybe you should start by experimenting with simple LEDs project before. Arduino is an amazing community constantly growing and creating tools for beginners and advanced programmers so if you need help all the things you are looking for are right there: https://www.arduino.cc/

Leave a Reply

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