Get tutorials Raspberry Pi Sensor Kit V2.0 for Raspberry Pi B+

Lesson 9 IR Receiver Module

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

- 4 * Jumper wires (Male to Male, 2 red and 2 black)

- 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

T-Cobbler

IR Receiver Module

GPIO0

GPIO17

SIG

5V

5V0

VCC

GND

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 Code


#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()


 

 

 


Copyright © 2012 - 2016 SunFounder. All Rights Reserved.


Previous chapter: Lesson 8 Vibration Switch

Next chapter: Lesson 10 Buzzer Module

SunFounder
Apr 14 2017 at 09:10 am



© Developed by CommerceLab