Introduction
An infrared-receiver (as shown below) is a component which receives infrared signals and can independently receive infrared rays and output signals compatible with TTL level. It is similar with a normal plastic-packaged transistor in size and is suitable for all kinds of infrared remote control and infrared transmission.
Components
– 1 * Raspberry Pi
– 1 * Breadboard
– 1 * Network cable (or USB wireless network adapter)
– 1 * IR receiver module
– 1 * IR Remote Controller
– 1 * 3-Pin anti-reverse cable
Experimental Principle
In this experiment, send signals to IR receiver by pressing buttons on the IR remote controller. The counter will add 1 every time it receives signals; in other words, the increased number indicates IR signals are received. The schematic diagram of the module is as shown below:
Experimental Procedures
Step 1: Build the circuit
Raspberry Pi | IR Receiver Module |
GPIO0 | SIG |
5V | VCC |
GND | GND |
For C language users:
Step 2: Change directory
cd /home/pi/SunFounder_SensorKit_for_RPi2/C/09_ir_receiver/
Step 3: Compile
gcc ir_receiver.c –lwiringPi
Step 4: Run
sudo ./a.out
For Python users:
Step 2: Change directory
cd /home/pi/SunFounder_SensorKit_for_RPi2/Python/
Step 3: Run
sudo python 09_ir_receiver.py
Here you can see the LED on the module blinking, and “Received infrared. cnt = xxx” will be printed on the screen.
C Code3
#include <wiringPi.h>
#include <stdio.h>
#define IR 0
int cnt = 0;
void myISR(void)
{
printf("Received infrared. cnt = %d\n", ++cnt);
}
int main(void)
{
if(wiringPiSetup() == -1){ //when initialize wiring failed,print messageto screen
printf("setup wiringPi failed !");
return 1;
}
if(wiringPiISR(IR, INT_EDGE_FALLING, &myISR) == -1){
printf("setup ISR failed !");
return 1;
}
//pinMode(IR, INPUT);
while(1);
return 0;
}
Python Code
#!/usr/bin/env python
import RPi.GPIO as GPIO
IrPin = 11
count = 0
def setup():
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(IrPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def cnt(ev=None):
global count
count += 1
print 'Received infrared. cnt = ', count
def loop():
GPIO.add_event_detect(IrPin, GPIO.FALLING, callback=cnt) # wait for falling
while True:
pass # Don't do anything
def destroy():
GPIO.cleanup() # Release resource
if __name__ == '__main__': # Program start from here
setup()
try:
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destroy()