Wednesday, September 21, 2016

Use any infrared remote to control an RGB led with an arduino uno [beginner]



This is a very simple tutorial article for those who want to start using an infrared remote with an arduino for controlling different things. This will cover the basic principles and can be used to perform complicated things as in my previous posts here and here

Things needed


1) An arduino uno or compatible boards (atmega328 based)
2) IR Receiver Module 38 kHz TSOP4838  (ebay)
3) 3 Colour RGB  LED Module
4) A TV remote or any ir remote
5) Connecting wires


Connecting things


Connections are pretty simple, being beginners tutorial i will explain a bit: See the figure below



IR module has a vcc and gnd pin which is connected to 5volt pin and ground pin on the arduino. Similarly there are four connections on the rgb led, the common ground is connected to ground (-ve) pin on arduino and each colour is connected to digital pins (D9, 10 and 11 respectively)

Uploading the firmware

Now connect the usb cable and connect it to the computer with arduino software. It needs a library for infrared remotes which can be downloaded from here. After setting up the library paste the source code below and upload to the arduino.

Now after uploading the code and if you have an nec remote, pressing 7, 8, and 9 will turn on each colors on the rgb led and 4,5,and 6 will turn it off. If nothing happens and probably you have some other remote controls, just open a serial monitor  (it is a window on the arduino software, see here) and set it to 9600 and start pressing the remote buttons to see some numbers appearing over there. Just replace those numbers in the section below ( complete source code at the end of the article)

     if(results.value==3782926367)  // number 7
     digitalWrite(led, HIGH);  
     else if (results.value==3782877407)  //number 4
     digitalWrite(led, LOW);  
     // result  
     if(results.value==3782873327)  //number 8
     digitalWrite(blue, HIGH);  
     else if (results.value==3782910047) //number 5 
     digitalWrite(blue, LOW);  
     // result  
     if(results.value==3782905967)  //number 9
     digitalWrite(green, HIGH);  
     else if (results.value==3782893727)  //number 6
     digitalWrite(green, LOW);  

See the video :


Source code 


 /* Include the IRremote library */  
 #include <IRremote.h>  
 /* Define the DIO pin used for the receiver */  
 #define RECV_PIN 5  
 /* Structure containing received data */  
 decode_results results;  
 /* Used to store the last code received. Used when a repeat code is received */  
 unsigned long LastCode;  
 /* Create an instance of the IRrecv library */  
 IRrecv irrecv(RECV_PIN);  
 int led = 9;  
 int green = 10;  
 int blue = 11;  
 void printHex(int num, int precision) {  
   char tmp[16];  
   char format[128];  
   sprintf(format, "0x%%.%dX", precision);  
   sprintf(tmp, format, num);  
   Serial.print(tmp);  
 }  
 void setup()  
 {  
   /* Configure the serial port to display the received codes */  
   Serial.begin(9600);  
   /* Start receiving codes */  
   irrecv.enableIRIn();  
   irrecv.blink13(1);  
   /* Initialise the variable containing the last code received */  
   LastCode = 0;  
   pinMode(led, OUTPUT);  
   pinMode(blue, OUTPUT);  
   pinMode(green, OUTPUT);  
 }  
 /* Main program */  
 void loop()  
 {  
   /* Has a new code been received? */  
   if (irrecv.decode(&results))  
   {  
     /* If so get the button name for the received code */  
     //Serial.println(GetIRIndex(results.value));  
     Serial.println(results.value);  
     //printHex(results.value,7);  
     if(results.value==3782926367)  
     digitalWrite(led, HIGH);  
     else if (results.value==3782877407)  
     digitalWrite(led, LOW);  
     // result  
     if(results.value==3782873327)  
     digitalWrite(blue, HIGH);  
     else if (results.value==3782910047)  
     digitalWrite(blue, LOW);  
     // result  
     if(results.value==3782905967)  
     digitalWrite(green, HIGH);  
     else if (results.value==3782893727)  
     digitalWrite(green, LOW);  
     /* Start receiving codes again*/  
     irrecv.resume();  
   }  
 }  


Sunday, September 18, 2016

Simple and quick way to test a 28BYJ-48 Stepper motor using arduino [beginner]

When you need to move something with your micro-controllers (or arduinos), Stepper motors comes to your rescue. One of the cheapest and widely used stepper motor is 28BYJ-48 which can work with a 5v supply and gives enough juice for small tasks like moving a small robot or a small lever or a valve.

Quick start with a 28BYJ-48 with an arduino

Quickly test the Stepper motor with an arduino

Wiring is very simple. It is important to use a stepper controller board using ULN2003 stepper driver which basically deliver enough current to stepper motor (as arduino pin has limitations on current drain). Connect it as shown in figure and basically 4 digital pins are needed (here to test use D8, D9, D10, D11) and a separate line to provide the vcc with enough current to run the stepper. For testing with no load, you can connect 5v pin from arduino to the vcc pin on stepper.

Upload the sketch below and if the stepper is fine it will start turning back and forth.

Idea is to keep it simple to just test the stepper after taking out of its package. I am going to use it in a small automated antenna tuner and more about it can be read in my next post

Arduino stepper motor quick start

Source Code :


/*
  Connect to stepper
  In addition add powersupply for stepper (GND & 5 to 12v as in figure
  shown in http://blog.riyas.org)
  
   IN1 >> D8
   IN2 >> D9
   IN3 >> D10
   IN4 >> D11

  */

#define IN1  8
#define IN2  9
#define IN3  10
#define IN4  11

int Steps = 0;
boolean step_dir = true;// gre
unsigned long last_time;
unsigned long currentMillis ;
int steps_left=4095;
long time;
void setup()
{
Serial.begin(115200);
pinMode(IN1, OUTPUT); 
pinMode(IN2, OUTPUT); 
pinMode(IN3, OUTPUT); 
pinMode(IN4, OUTPUT); 
// delay(1000);

}
void loop()
{
  while(steps_left>0){
  currentMillis = micros();
  if(currentMillis-last_time>=1000){
  stepper(1); 
  time=time+micros()-last_time;
  last_time=micros();
  steps_left--;
  }
  }
  Serial.println(time);
  Serial.println("Pause for 2 second");
  delay(2000);
  step_dir=!step_dir;
  steps_left=4095;
}

void stepper(int xw){
  for (int x=0;x<xw;x++){
switch(Steps){
   case 0:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, HIGH);
   break; 
   case 1:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, HIGH);
     digitalWrite(IN4, HIGH);
   break; 
   case 2:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, HIGH);
     digitalWrite(IN4, LOW);
   break; 
   case 3:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, HIGH);
     digitalWrite(IN3, HIGH);
     digitalWrite(IN4, LOW);
   break; 
   case 4:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, HIGH);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break; 
   case 5:
     digitalWrite(IN1, HIGH); 
     digitalWrite(IN2, HIGH);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break; 
     case 6:
     digitalWrite(IN1, HIGH); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break; 
   case 7:
     digitalWrite(IN1, HIGH); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, HIGH);
   break; 
   default:
     digitalWrite(IN1, LOW); 
     digitalWrite(IN2, LOW);
     digitalWrite(IN3, LOW);
     digitalWrite(IN4, LOW);
   break; 
}
Setstep_dir();
}
} 


void Setstep_dir()
{
if(step_dir==1){ Steps++;}
if(step_dir==0){ Steps--; }
if(Steps>7){Steps=0;}
if(Steps<0){Steps=7; }
}