Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Make your likes visible on Facebook?

Connect your Facebook account to Prezi and let your likes appear on your timeline.
You can change this under Settings & Account at any time.

No, thanks

Arduino

No description
by

Rishabh Jangir

on 26 January 2015

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Arduino

A Binary Counter
What is an Arduino/Induino?
Let's take a step back and this time try to blink a single LED
void setup() // Executed only once at Power On / Reset
{

pinMode(11,OUTPUT); // Initialises Pin 11 as an OUTPUT Pin

}

void loop() // keeps on running infinitely till the board is powered off
{

digitalWrite(11,HIGH); // Turns Pin 11 ON
delay(1000); // Waits for 1 second (1000 milliseconds = 1 second)
digitalWrite(11,LOW);// Turns Pin 11 OFF
delay(1000); // Waits for 1 second (1000 milliseconds = 1 second)

}

Review The Binary Counter
void setup()
{
pinMode(11,OUTPUT); // declare LED pins as output pins
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
}
void loop()
{
for(int i=0;i<8;i++) // increment automatically from 0 to 7 , i is the counter variable
{
int a=i%2; // calculate LSB
int b=i/2 %2; // calculate middle bit
int c=i/4 %2; // calculate MSB
digitalWrite(11,a); // write MSB
digitalWrite(12,b); // write middle bit
digitalWrite(13,c); // write LSB
delay(1000); // wait for a second
}
}
Working with the three On-board push buttons
Digital Inputs
Digital Inputs helps us interface devices that ouput a HIGH / LOW signal. There are a lot of devices - push buttons, proximity sensors, hall effect sensors, etc that output a digital signal. These devices can be connected to any of the Digital I/O pins (0-19).

Controlling the RGB LED

Analog Outputs
Analog Outputs help us product a varying range of voltages between 0 & 5 volts. This is very much useful in achieving intensity control. We can use analog outputs to control the intensity of a LED or to control the speed of a motor.

The Induino R3 Board has 6 Analog output pins. These are pins 3, 5, 6, 9, 10 & 11 and are marked PWM. The On-Board RGB LED is connected to Pins 3(Blue), 5(Green) & 6(RED).
Assignment question
changing LED colour and intensity
Serial communication

Serial Communication

The Induino R3 / Arduino Boards, provide a number of facilities for communicating with a computer, another Arduino, or other microcontrollers. The ATmega328 provides UART TTL (5V) serial communication, which is available on digital pins 0 (RX) and 1 (TX). A FT232RL IC on the board channels this serial communication over USB and appears as a virtual com port to the computer. The Arduino software includes a serial monitor which allows simple textual data to be sent to and from the Arduino board. The RX and TX LEDs on the board will flash when data is being transmitted via the USB-to-serial chip. This Serial Communication channel is used by the Arduino IDE to program the microcontroller. We can make use of the same channel to send run-time data to the board from a connected computer / device or to send run-time data from the board to a connected device (helpful in debugging).
GOAL!
Arduino
or let's just work with an Induino for the time being
Connect your Induino to your laptop via the UART cable.
Well, you might not have a Mac
INDUINO
UART cable
any computer would do.
Install the Arduino IDE
Install the Drivers
For Windows 8/8.1 Users
You have to disable driver signature reinforcement.
Follow these steps
Enable the drivers
and, we are good to go.
A Binary Counter using LED
A Binary counter is a simple counter that displays the current counter value in binary format. we have 3 On-board LEDs connected to Pins 11,12 &13 on the Induino R3, we can use these to build a 3-bit binary counter that will automatically increment every second and reset once it reaches 7.

We can assume the LED connected to PIN 11 to be the LSB [Least Significant Bit] and the LED connected to PIN 13 as the MSB [Most Significant Bit] . We can have a counter variable that we can increment till 7 and reset to 0 on reaching 7.

Now Lets take a look at the logic

Heres how a Binary counter will work, for each value of the variable Counter(0 - 7), the same shall be displayed in the Binary Format.

0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
0 0 0
...... and on
void setup()
{
pinMode(11,OUTPUT); // declare LED pins as output pins
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
}
void loop()
{
for(int i=0;i<8;i++) // increment automatically from 0 to 7 , i is the counter variable
{
int a=i%2; // calculate LSB
int b=i/2 %2; // calculate middle bit
int c=i/4 %2; // calculate MSB
digitalWrite(11,a); // write MSB
digitalWrite(12,b); // write middle bit
digitalWrite(13,c); // write LSB
delay(1000); // wait for a second
}
}
The source code
So Whats Arduino All About?

Arduino is a Open-Source Prototyping Platform - Its a set of Open-Source (Design Files are available OPENLY for modification) Tools a.ka Platform (Hardware + Software for Programming the Hardware) that can help you build prototypes easily!

Arduino is tool for making programmable computers(embedded systems) for specific applications. Using these which you can sense and control different real-world parameters.

Then what is an Induino?
The Induino R3 is a Indian made Clone of the popular
Arduino Duemilanove Board with a Arduino Uno Bootloader. In other words in the above description of Tools, it is the hardware board. It can be programmed using the Arduino Software and would behave exactly like an Arduino Board.


The Induino R3 is a low-cost Indian made clone / derivative of the popular Arduino Board.
Hardware
Use webcam to show the components

The Induino R3 / Arduino Uno Boards have 20 programmable I/O's. These are Pins 0 to 13 and Pins 0 to 5 [Analog In 0 to 5]. For reference sake, we will refer to the pins Analog In 0 to 5 as A0 to A5

Digital I/O's
All of the 20 I/O's can be used for working with Digital Signals - They can both take Digital Inputs & produce Digital Ouput. The Pins marked A0 to A5 [Analog In 0 to 5] When being used for working with Digital Inputs / Outputs are referred to as 14 to 19. So when working with Digital Inputs / Outputs, the pins are numbered from 0 to 19 (0 to 13 & 14(A0) to 19(A5)). The Digital Pins can be used for controlling LED's, Relays, Etc and for Taking input from Push-Buttons, Digital Sensors, Etc.

Analog I/O's
Analog Input
The pins A0 to A5 [Analog In 0 to 5] are pins dedicated for working with Analog Inputs. They have in inbuilt Analog to Digital Converter that will convert analog voltages in the range of 0 to 5 volts to a 10-bit value. They can be used for working with Analog Sensors - Temperature, Light, Etc.

Analog Output
The six pins 3,5,6,9,10 & 11 marked PWM are pins dedicated to produce Analog Output Signal. They can produce analog voltages in the range of 0 to 5 volts with a resolution of 8-bits. They can be used for Intensity Control, Speed Control, Etc.

External Interrupts
There are 2 external Interrupts available on the Induino R3 / Arduino Uno Boards. These are on pins 2 & 3. These pins are used to configure external interrupts. They can be used to connect digital inputs like alarm switch, etc.
MEMORIES

There are three types of Memories available on the ATmega328

Flash memory (32KB, of which .5k is used for the bootloader) - is where the program is stored.
SRAM (static random access memory - 2KB) - is where the program creates and manipulates variables when it runs.
EEPROM (1 KB) - is memory space that programmers can use to store long-term information.


Communication

Serial Communication
I2C Communication a.k.a Two Wire Communication
SPI Communication
Software Serial

Push Buttons
Push Buttons are momentary switches - they work as a switch when they are being pressed.

Controlling One LED from Push Button
When LED state= Button State

int switchPin=7;
int LEDpin=12;
boolean LEDon=false;
boolean prevButtonState=HIGH;

void setup(){
pinMode(7, INPUT);
pinMode(12, OUTPUT);
digitalWrite(7, HIGH);
}

void loop(){

if(digitalRead(switchPin)==HIGH && prevButtonState==LOW){
LEDon=!LEDon;
prevButtonState=HIGH;
}
else{
prevButtonState=digitalRead(switchPin);
}

digitalWrite(LEDpin, LEDon);
}
The other case.
With and without debouncing
int switchPin=7;
int LEDpin=12;
boolean LEDon=false;
boolean prevButtonState=HIGH;
boolean currentButtonState;
void setup(){
pinMode(7, INPUT);
pinMode(12, OUTPUT);
digitalWrite(7, HIGH);
}

boolean debounce(boolean prev){
boolean current= digitalRead(7);
if(current!=prev){
delay(5);
current=digitalRead(7);
}
return current;
}

void loop(){
currentButtonState=debounce(prevButtonState);
if(currentButtonState==HIGH && prevButtonState==LOW){
LEDon=!LEDon;
}
prevButtonState=currentButtonState;
digitalWrite(LEDpin, LEDon);
}
























Push Button controlled Binary counter
int i = 0;
void setup()
{
pinMode(11,OUTPUT); // declare LED pins as output pins
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
pinMode(7,INPUT);// declare the Button as INPUT pin with internal Pull up enabled
digitalWrite(7,HIGH);
}
void loop()
{
if(digitalRead(7)==0) // if the button is pressed
{
if(i<7) // if counter value is less than 7 or 3 bits
i++; // increment counter value
else
i=0; // reset counter to 0
int a=i%2; // calculate LSB
int b=i/2 %2; // calculate middle bit
int c=i/4 %2; // calculate MSB
digitalWrite(11,a); // write LSB
digitalWrite(12,b); // write middle bit
digitalWrite(13,c); // write MSB
while(digitalRead(7)==0); // wait till button is released to avoid incrementing the counter again
delay(100); // small delay to avoid debounce
}
}

Resolution

Analog signals (both Output & Input) can be a varying range of voltages between 0 & 5 Volts.. Try now to answer this question for a second: how many voltage values are there between 0 and 5 volts? The answer is simple: endless. Imagine two voltage values in the range we are working with, that are as close as possible, e.g. 3.4 and 3.41 volts. It is possible to have endless values inbetween those two: 3.401, 3.403, 3.402539 ...
This means that we would need a processor with the ability to represent endless numbers, but Arduino cannot. Therefore, we speak about Resolution. Resolution defines the finest amount of change that is possible. In Arduino, the Analog Output resolution is 8-bits and the Analog Input resolution is 10-bits.
Multiple Colours through same LED
void setup()
{ // Leave the setup() function empty
}
void loop()
{
analogWrite(3,153);// Setting the voltage for Blue to around 3 Volts
analogWrite(5,51);// Setting the voltage for Green to around 1 Volt
analogWrite(6,51);// Setting the voltage for Red to around 1 Volt
}
Controlling the intensity
byte intensity = 0; // byte is a variable type that can hold 1 byte (8 bits) of data

void setup()
{
pinMode(7,INPUT); // Increment Button
pinMode(9,INPUT); // Decrement Button
digitalWrite(7, HIGH);
digitalWrite(9, HIGH);
}
void loop()
{
if(digitalRead(7)==0) // Check if the increment button is being pressed
{
if(intensity<255) // Check if the intensity is within permissible limits for incrementing
intensity++; // increment intensity
else
intensity = 0; // if it has reached the limit, reset it to 0
delay(20); // Note we are not waiting for the button to be released but only having a delay.
}
if(digitalRead(9)==0) // Check if the decrement button is being pressed
{
if(intensity>0) // Check if the intensity is within permissible limits for decrementing
intensity--; // decrement intensity
else
intensity = 255; // if it has reached the limit, reset it to 255
delay(20); // Note we are not waiting for the button to be released but only having a delay.
}

analogWrite(6,intensity); // write the intensity value to the Analog Pin 6 (RED)
}
byte RGB[3]; // Variable to store individual Color Value of Different Colors of the RGB LED
int arr_ind=0; // Variable for Navigating through the Above Array - An Aray Index Variable

void setup()
{
pinMode(7,INPUT); digitlWrite(7,HIGH);
pinMode(8,INPUT); digitlWrite(8,HIGH);
pinMode(9,INPUT); digitlWrite(9,HIGH);

pinMode(11,OUTPUT);//LED for Current Selected Color Indication for RED Color - Lights up When the User Selects RED Color
pinMode(12,OUTPUT);//LED for Current Selected Color Indication for BLUE Color - Lights up When the User Selects BLUE Color
pinMode(13,OUTPUT);//LED for Current Selected Color Indication for GREEN Color - Lights up When the User Selects GREEN Color

RGB[0] = 0; // RGB[0] will store the value for the BLUE Color
RGB[1] = 0; // RGB[1] will store the value for the RED Color
RGB[2] = 0; // RGB[2] will store the value for the GREEN Color

applyColor(); // Calling a Function that will handle the AnalogWrite functions for the RGB LED
}

void loop()
{
if(digitalRead(7)==0) // Checking if the Increment button is Being Pressed, If True, the value of the currently selected color's value is incremented
{
if(RGB[arr_ind]<255) // Checks if the currently selected color value is lesser than 255 before incrementing. So when it reaches 255, the value is reset to 0.
RGB[arr_ind]++;
else
RGB[arr_ind]=0;
delay(100);
}
if(digitalRead(9)==0)// Checking if the Decrement button is Being Pressed, If True, the value of the currently selected color's value is decremented
{
if(RGB[arr_ind]>0)// Checks if the currently selected color value is greater than 0 before decrementing. So when it reaches 0, the value is reset to 255.
RGB[arr_ind]--;
else
RGB[arr_ind]=255;
delay(100);
}
if(digitalRead(8)==0)// Checking if the color button is Being Pressed, If True, the value of the array index is incremented to the next value
{
if(arr_ind<2)
arr_ind++;
else
arr_ind=0;
while(digitalRead(8)==0); // This while is used to debounce the button press or in other words, wait for the user to release the button
delay(50);
}
if((digitalRead(7)==0)&&(digitalRead(9)==0))// Checking if both the increment & decrement buttons are being pressed at the same time. If so, all color values are reset to zero
{
RGB[0] = 0; // RGB[0] will store the value for the BLUE Color
RGB[1] = 0; // RGB[1] will store the value for the RED Color
RGB[2] = 0; // RGB[2] will store the value for the GREEN Color
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,HIGH);// This to indicate a reset in progress. All three LEDS GLOW for 200 milliseconds and go Off
delay(200);
digitalWrite(11,LOW);
digitalWrite(12,LOW);
digitalWrite(13,LOW);
}

switch(arr_ind) // The switch is used to indicate the current color selection through the corresponding LED based on the current value of the Array Index
{
case 0:
digitalWrite(11,LOW);
digitalWrite(12,HIGH);
digitalWrite(13,LOW);
break;
case 1:
digitalWrite(11,HIGH);
digitalWrite(12,LOW);
digitalWrite(13,LOW);
break;
case 2:
digitalWrite(11,LOW);
digitalWrite(12,LOW);
digitalWrite(13,HIGH);
break;
}
applyColor();// Calling a Function that will handle the AnalogWrite functions for the RGB LED

}

// The function applyColor() will apply the RGB array variable's current value to the Analog Pins 3,5 & 6 which control the RGB LED
void applyColor()
{
analogWrite(3,RGB[0]);
analogWrite(5,RGB[2]);
analogWrite(6,RGB[1]);
}
Lets see how we can print some data from our Board to the Computer. We will need a few functions to achieve this.


Serial.begin(buad rate)
=> The Serial.begin() function initiates a serial communication at the given baud rate. baud rate is the rate of data transfer in bits per second. For communicating with the computer, use one of these rates: 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200. This function is called from within the setup() function.


Serial.print(value)
=> The Serial.print() function prints the given value back to the connected device. The value can be a string, character or a variable.


Serial.println(value)
=> The Serial.println() function is similar to the Serial.print() function, only that it prints a new line character at the end of the line.

Here's a slight modification to our last program - Button controlled Binary Counter. On every Button press we shall print the current counter value. To view the output of this program, you need to used the Arduino IDE's Serial Monitor (Click the Lonely Icon on the Top Right Side or go to Tools - > Serial Monitor). In the Serial Monitor window, ensure that the baud rate selected (at the bottom right corner) is the same as the one used in the program)
int i = 0;
char txt[]="The Current Value is : "; // A string stored in a character array
void setup()
{
pinMode(11,OUTPUT); // declare LED pins as output pins
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
pinMode(7,INPUT);// declare the Button as INPUT pin
digitalWrite(7,HIGH);
Serial.begin(9600); // initialize Serial Communication
Serial.println("Starting the Program");// This will be printed only once
}
void loop()
{
if(digitalRead(7)==0) // if the button is pressed
{
if(i<7) // if counter value is less than 7 or 3 bits
i++; // increment counter value
else
i=0; // reset counter to 0
Serial.print(txt); // Print Descriptive test from the character array
Serial.println(i); // print the current value
int a=i%2; // calculate LSB
int b=i/2 %2; // calculate middle bit
int c=i/4 %2; // calculate MSB
digitalWrite(11,a); // write LSB
digitalWrite(12,b); // write middle bit
digitalWrite(13,c); // write MSB
while(digitalRead(7)==0); // wait till button is released to avoid incrementing the counter again
delay(100); // small delay to avoid debounce
}
}
Now that was sending data to the computer, what if we wanted to receive data from the computer?
Let's see how we can do this, first we would need a few more functions


Serial.available()
=> All Serial data coming into the microcontroller is stored in a serial buffer. The Serial.available() function returns the size of the available data in the Serial Buffer in BYTES. We can use this function to determine if there is incoming data or not. The Serial Buffer can hold upto 64 Bytes of Data

Serial.read()
=> The Serial.read() function reads 1 BYTE of data at a time and returns the BYTE value for the same. The moment a piece of data leaves your computer / microcontroller it loses its variable type and is simply treated as a BYTE(ASCII) value. When you send 'A' from your computer / microcontroller, the corresponding ASCII value 65, is what is sent out. Depending upon the variable type you store it in, it can read as either a character or a number. The 65 when stored in a character variable will be stored as 'A' and when stored in an integer variable will be stored as 65. once the Serial.read() functions reads a byte of data that byte of data will be removed from the Serial Buffer.

Now let's modify our last program for an added Input coming from the Serial Port of our computer. Let's say the user wants to change the current counter value of the binary counter, We cad do this by using Serial communication for input. Any time the user inputs a number between 0 & 7, we shall use it as the current counter value and display the same in the binary format using our binary counter.

Here's the outline of the logic

Keep checking for either button press / or data in the serial buffer.
On button press increase the counter and display the value on the leds
On serial data being received, read the byte, check if it is between 0 & & and then set the counter value to the value received. Finally display the counter value on the leds.


int i = 0;
char txt[]="The Current Value is : "; // A string stored in a character array
void setup()
{
pinMode(11,OUTPUT); // declare LED pins as output pins
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
pinMode(7,INPUT);// declare the Button as INPUT
digitalWrite(7,HIGH);
Serial.begin(9600); // initialize Serial Communication
Serial.println("Starting the Program");// This will be printed only once
}
void loop()
{
if(digitalRead(7)==0 || Serial.available()>0) // if the button is pressed or Serial data is received
{
if(digitalRead(7)==0) // if the button is pressed
{
if(i<7) // if counter value is less than 7 or 3 bits
i++; // increment counter value
else
i=0; // reset counter to 0
while(digitalRead(7)==0); // wait till button is released to avoid incrementing the counter again
delay(100); // small delay to avoid debounce
}
if(Serial.available()>0) // if Serial data is received

{
int val = Serial.read();
val = val - 48; // 48 is the ascii value for 0.
if(val>=0 && val <=7)// Check if Val is in the range of 0 to 7
{

i = val; // assign the counter to the value received from the Serial Port
}

}
Serial.print(txt); // Print Descriptive test from the character array
Serial.println(i); // print the current value
int a=i%2; // calculate LSB
int b=i/2 %2; // calculate middle bit
int c=i/4 %2; // calculate MSB
digitalWrite(11,a); // write LSB
digitalWrite(12,b); // write middle bit
digitalWrite(13,c); // write MSB

}
}
Analog Inputs - What, Where & How? - Working with the on-board LDR
Analog Inputs

There are 6 dedicated Analog Input pins on the Induino R3 that can be used for connecting Analog Input devices like sensors to the Induino R3 Board. These are the pins marked A0 - A5.

There are sensors which produce a voltage value as output to indicate the quantity being measured. The Analog Input Pins are connected to an internal Analog to Digital Converter, that will then convert these voltages to digital values. The resolution for Analog Input is 10-bits. (if you did not go through the previous tutorial, please go through it to understand resolution). So voltage values from 0 to 5 volts are represented using a numerical value in the range of 0 to 1023. If a sensor outputs, 1 volts, we would be getting a numerical value of 204. Owing to the 10-bit resolution, we can measure changes greater than 4.9 millivolts.

There is a LDR on the Induino R3 Board that is connected to pin A3. The LDR acts as a simple on-board light sensor.

LDR - Light Dependant Resistor - is a type of a variable resistor whose resistance varies with light incidence. Resistance of the LDR is inversely proportional to the light incident upon the LDR. When there is more light, resistance is less and when there is less light resistance is more. There are a lot of sensors out there that are variable resistors - Thermistor, Force Sensitive Resistor, etc.
Connecting Variable Resistors to the Induino R3 / Arduino Board

When connecting a variable resistor type sensor, we need to convert the change in resistance to change in voltage and then give this voltage as input to our microcontroller (same with all the microcontrollers) We can achieve this by using a potential divider configuration as in the following image. The Vout from the following circuit needs to be connected to the Analog Input pin. Vin is +5Volts. You can use the same configuration in connecting all variable resistor types of sensors. just replace the LDR in the circuit.
The LDR on the Induino R3 Board is connected using the First Setup in the above image. So you would get a Lesser Voltage Value when there is more light on top of the LDR and Higher Voltage Value when there is less light on top of the LDR.
analogRead(pin number)
- The analogRead() function will read the voltage value of the given pin and give us a value in the range of 0 to 1023.

int ldr_val = 0; // variable to store the LDR value

void setup()
{
// No pinMode Statement required for Analog Pins :)
Serial.begin(9600); // Initialise Serial Communication
}

void loop()
{
ldr_val = analogRead(3); // Read the ldr value and store it in the variable
Serial.print("Current LDR Value : ");
Serial.println(ldr_val); // print the ldr value to serial monitor
delay(1000); // a delay
}
#define threshold1 650 // First Threshold Value of Darkness above which the first LED is switched ON
#define threshold2 750 // Second Threshold Value of Darkness above which the second LED is switched ON
#define threshold3 950 // Third Threshold Value of Darkness above which the third LED is switched ON
int val = 0; // A Variable to Store the Light Value from the LDR
void setup()
{
pinMode(11,OUTPUT); // LED 1
pinMode(12,OUTPUT); // LED 2
pinMode(13,OUTPUT); // LED 3
Serial.begin(9600);// Start a Serial Connection
}
void loop()
{
val = analogRead(3);// Reads a 10-bit value corresponding to the voltage applied on analog input pin 3.
Serial.print("Light Intensity is : ");// Prints the given string / value to the serial monitor
Serial.println(val);// prints the value of the variable val to the serial monitor and moves the cursor to the next line (the ln part of println does this
if(val > threshold1) // Checks & Turns the First LED ON / OFF based on Light Intensity
digitalWrite(11,HIGH);
else
digitalWrite(11,LOW);
if(val > threshold2) // Checks & Turns the Second LED ON / OFF based on Light Intensity
digitalWrite(12,HIGH);
else

digitalWrite(12,LOW);
if(val > threshold3) // Checks & Turns the Thirdf LED ON / OFF based on Light Intensity
digitalWrite(13,HIGH);
else
digitalWrite(13,LOW);
}
Light LED based on analog input
Analog Input from the LDR
Wireless data transfer
The on-board IR LED
We have an on-board IR LED on the Induino R3 Board. When we were designing the first Induino Board, we thought it would be fun to be able to control a television from the board and ended up adding the IR LED to the board. On top of being able to control devices with remote control, the IR LED can be combined with the TSOP to build a simple wireless communication system. However to try the wireless communication system, you would require two Induino R3 Boards.

The IR LED is connected to digital pin 14 (A0). A Few things to note about the IR LED

It is not a visible spectrum LED, so you will not be able to see it glowing with your naked eyes, if you look at the IR LED through a camera, you will be able to spot a Pink glow - that's the IR LED glowing.
The IR LED on the Induino R3 board is driven by a simple driver circuitry and might not be as powerful as the one in your remote. You might have to point it in a straight line to get it working.

Understanding Frequency
The Sony SIRC protocol works at 40Khz...most remote controls work at their specific frequencies. TV remotes in India are generally between 32- 40Khz frequecny.

So whats this frequency all about?
40KHz means 40,000 Cycles in 1 second. Each Cycle represents a ON signal followed by a OFF signal. The signals could be of equal duration.
Lets do some calculation,
40,000 Cycles in 1 Seconds => 1000 milliseconds => 1000 x 1000 microseconds
so each cycle if for a duration of 25 microseconds.

We can produce this frequency if we can make a pin go high for 13 microseconds followed by low for 12 microseconds.

If we can do this for 2.4 milliseconds then we can generate a Start bit, if we can do this for 1.2 milliseconds then we can generate a Logical '1', and for 0.6 milliseconds, we can generate a Logical '0'.

Now that we know that each cycle will take 25 microseconds lets calculate the number of cycles required for generating the 2.4 milliseconds start bit

2.4 milliseconds / 25microseconds => 2400 microseconds / 25 microseconds => 96 cycles
1.2 milliseconds / 25microseconds => 1200 microseconds / 25 microseconds => 48 cycles
0.6 milliseconds / 25microseconds => 600 microseconds / 25 microseconds => 24 cycles

If you noticed, we are working with microseconds, we would need a new function to work with these

delaymicroSeconds(time in microseconds) => the delayMicroseconds() function takes a parameter and produces a corresponding delay in microseconds.
Now Here's how we are going to go ahead with our Programming

Take the value to be transmitted convert it to a 12-bit binary value and store the binary value in an array
Process the array going though 1 element at a time, and based on the element ('1' or '0') called a function to control the IR led for a given number of cycles

Since we will be working with Binary Numbers, We shall use the boolean data type.
boolean - boolean is a datatype used for storing values '1' or '0' (true or false)

Let's first write the function to control the IR LED to pulse for a given number of cycles. Here's the function. The transmit() function will take the number of cycles as a parameter and create a 40KHZ Pulse for that many cycles. Now the Cycle time for each cycle has to be 25 microseconds, keeping this in mind we will have to compensate for the time taken by each statement to execute as well
void transmit(int number)
{
// each execution of this 'for loop' takes 10 microseconds, so our delay timings are adjusted accordinly
// each execution of this 'for loop' should correspond to the duration of 1 cycle
// for Sony protocol our cycle duration is 25microseconds, so we have delays of 8 + 7 + 10(total statement execution time for all statements inside)
for(int i=0; i<number;i++)
{
digitalWrite(14,HIGH);
delayMicroseconds(8);
digitalWrite(14,LOW);
delayMicroseconds(7);
}
delayMicroseconds(600); // this is the delay corresponding the the Space timing between 2 pulses
}
THE TRANSMIT FUNCTION
void cnvrt_n_snd(int data)
{
// The first for loop is for converting the data into a 12-bit binary value and storing it in an array
for(int i=0;i<12;i++)
{
binary_array[i] = data % 2; // get the reminder and store in an array
data= data/2;
}
// First send the Startbit, Then navigate through the Array, check each bit value and transmit a signal accordingly
transmit(96); // Transmit Start Bit
// If you are using the IR Remote Library to receive at the receiver change the for loop below like this for(int i=11;i>=0;i--)
for(int i=0;i<12;i++)
{
if( binary_array[i]== 1) // Check if the Binary value is '1' if so trasnmit the signal for '1' else transmit the signal for '0'
transmit(48);
else
transmit(24);
}
}
CONVERTING DATA INTO BINARY
Write a program to send the number 144 wirelessly
boolean binary_array[12]; // A boolean array for storing the converted binary values

void setup()
{
pinMode(14,OUTPUT); // define the IR LED pin as output
}

void loop()
{
cnvrt_n_snd(144); // Transmitting the Value of the Ch UP button
delay(1000);
}

void transmit(int number)
{
// each execution of this 'for loop' takes 10 microseconds, so our delay timings are adjusted accordinly
// each execution of this 'for loop' should correspond to the duration of 1 cycle
// for Sony protocol our cycle duration is 25microseconds, so we have delays of 8 + 7 + 10(total statement execution time for all statements inside)
for(int i=0; i<number;i++)
{
digitalWrite(14,HIGH);
delayMicroseconds(8);
digitalWrite(14,LOW);
delayMicroseconds(7);
}

delayMicroseconds(600); // this is the delay corresponding the the Space timing between 2 pulses
}

void cnvrt_n_snd(int data)
{
// The first for loop is for converting the data into a 12-bit binary value and storing it in an array
for(int i=0;i<12;i++)
{
binary_array[i] = data % 2; // get the reminder and store in an array
data= data/2; // Divide the number by 2
}
// First send the Startbit, Then navigate through the Array, check each bit value and transmit a signal accordingly
transmit(96); // Transmit Start Bit
// If you are using the IR Remote Library to receive at the receiver change the for loop below like this for(int i=11;i>=0;i--)
for(int i=0;i<12;i++)
{
if( binary_array[i]== 1) // Check if the Binary value is '1' if so transmit the signal for '1' else transmit the signal for '0'
transmit(48);
else
transmit(24);
}
}
Write a program to send the analog input form the LDR wirelessly to the other induino
Wireless data transfer
The Transmitter
The Receiver
TSOP Remote Control Receiver
The TSOP SM0038 is an IR receiver on the InduinoX. The TSOP will help you to interface your TV remote with the InduinoX and in the Process learn the basics of Wireless Communication. On the Induino R3, the TSOP is connected to digital pin 15(A0).

The TSOP outputs a constant HIGH signal when idle and as it receives data, it tends to invert the data. i.e when an IR LED is transmitting data onto the TSOP, everytime the IR led goes high, the TSOP will go LOW and vice versa. Remote control signals are often bytes of data that is encoded and transmitted by pulsing(switching ON & OFF the IR LED at a specific frequency) Most TV remote controls work at 32-40 Khz frequency and most receivers can receive this range.

Here's a basic outline of how the data is sent. Every time you press a button on a Sony remote control, it sends out a 13Bit data. The first bit is a start bit indicating there are 12 bits of data following it. The next 7 bits are the command bit which will vary depending upon the keys being pressed. The last 5 bits are the address bits which will the same for all buttons but vary for remote controls of different devices.The black bars in the following image correspond to high signals (called marks) and the white spaces in between correspond to low signals (called spaces). The duration of the 'marks' varies according to the bit being transmitted. It is 2.4ms for the start bit, 1.2ms for HIGH bit and 0.6ms for LOW bit. The duration of the 'spaces' is a constant 0.6ms. Every mark is followed by a space. Any data can be converted to binary format and transmitted in this manner. In fact this is the basic form of all types of serial communication.
Technique to decode this signal train, would be to constantly monitor the TSOP pin[15] for its normal state and the moment it produces a low signal, measure the duration of the low signal. If the measured duration of the low signal is around 2ms then measure and store the duration for the next 12 bits of the incoming data. After storing the data, evaluate the duration and based on the duration convert the data to decimal / hexadecimal and use it in your application.

The duration of a signal on an input pin of the arduino can be measured using the pulseIn() function.
Now Here's how we are going to go ahead with decoding

Keep checking the TSOP pin (Pin 15) for a LOW pulse of duration in excess of 2ms, the moment you get such a signal proceed on to step 2
Run a 'for' loop for 12 counts, during each iteration of the loop, get the current pulse duration using the pulseIn function. Check if the duration is greater than 1000ms (means its a '1') else its a '0'
As soon as you detect a '1' or '0' add it to an appropriate binary to decimal conversion logic.
We will process the whole signal as a 12-bit data rather than process the address / command data separately. We will consider the first data received as the LSB. These 2 aspects might change depending upon your application. Here our objective is to use a TV remote to control our board.

void setup()
{
pinMode(15,INPUT); // TSOP is connected on the 15ht pin
Serial.begin(9600);
}

void loop()
{
int remote_val = remote(); // Call the remote function to get the value of the key pressed
if(remote_val>0) // check if the value is greater than 0. A 0 means that no signal was received by the TSOP
{
Serial.println(remote_val);
delay(150); // A remote press will normally generate 3 signal trains. This is to avoid reading duplicates
}
}


// A Dedicated function that will calculate and return a decimal value for each of the buttons on a remote.
int remote()
{
int value = 0; // a Variable to store our final calculated value
int time = pulseIn(15,LOW); // we need to look for the duration of the LOW pulse as TSOP will invert the incoming HIGH pulse
if(time>2000) // Checking if the Start Bit has been received. Start Bit Duration is 2.4ms
{

for(int counter1=0;counter1<12;counter1++) // A loop to receive the next 12 bits The loop is in the inverse order as we will be receiving the MSB
{
if(pulseIn(15,LOW)>1000) // checking the duration of each pulse, if it is a '1' then we use it in our binary to decimal conversion, '0's can be ignored.
{
value = value + (1<< counter1);// binary to decimail conversion. 1<< i is nothing but 2 raised to the power of i
}
}
}
return value;
}

Powers of 2
In the above program, we used a left shift (<<) operator to find the powers of 2. The Left shift operator, shifts the bits in the given number by the given number of places and fills in zeroes in the right most given number of places.

(given number) << (given number of places)

Lets try a few examples,
lets shift 10 by 2 places => 10 << 2 => 00001010 << 2 => 00101000 => 40 => 10 x (2 power 2)

lets shift 10 by 4 places => 10 << 4 => 00001010 << 4 => 10100000 => 160 => 10 x (2 Power 4)

So as we can see left shift produces multiplication of the given number by powers of 2. If the given number was 1, then we will end up with the powers of 2.
int i = 0; // the countervalue for the binary counter
void setup()
{
pinMode(11,OUTPUT); // declare LED pins as output pins
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
pinMode(15,INPUT); // declare the tsop pin as Input
}
void loop()
{
int remote_val = remote(); // Call the remote function to get the value of the key pressed
if(remote_val>0) // check if the value is greater than 0. A 0 means that no signal was received by the TSOP
{
if(remote_val == 144) // 144 is the Channel UP Button Value on the Sony Remote provided as part of the Induino R3 Learners Kit
{
if(i<7) // if counter value is less than 7 or 3 bits
i++; // increment counter value
else
i=0; // reset counter to 0
}
if(remote_val == 144) // 145 is the Channel UP Button Value on the Sony Remote provided as part of the Induino R3 Learners Kit
{
if(i>0) // if counter value is greater than 0 or 3 bits
i--; // decrement counter value
else
i=7; // reset counter to 7
}

int a=i%2; // calculate LSB
int b=i/2 %2; // calculate middle bit
int c=i/4 %2; // calculate MSB
digitalWrite(11,a); // write MSB
digitalWrite(12,b); // write middle bit
digitalWrite(13,c); // write LSB
delay(150); // A remote press will normally generate 3 signal trains. This is to avoid reading duplicates
}
}
Write a program to read a sentence being transmitted from some other Induino.
Full transcript