Lesson 6 Infrared Transmitter

Share for us

Introduction

An infrared transmitter (as shown below) is a type of remote control devices. It can emit rays within a certain range through infrared transmitting tube so as to control signals. Infrared transmitter is widely used in consumer electronics, industry and communication, etc.

Components

– 1 * Raspberry Pi

– 1 * Network cable (or USB wireless network adapter)

– 1 * Infrared transmitter module

– Several jumper wires

Experimental Principle

Connect the IR transmitter module with the Raspberry Pi and make the module emit infrared rays by programming. Since infrared ray is invisible to naked eyes, you can use a camera to observe.

Experimental Procedures

Step 1: Build the circuit  

                       Raspberry Pi                              Infrared Transmitter

                             GPIO0 ————————————– S

                              GND ————————————— GND

Step 2: Edit and save the code (see path/Rpi_SensorKit_code/08_infrared/infrared.c)

Step 3: Compile

              gcc  infrared.c  -lwiringPi

Step 4: Run

              ./a.out

Now you can see by a camera the infrared diode on the module emit infrared rays.

(Note: Infrared rays are not visible, but can be captured by camera).

infrared.c

#include <wiringPi.h>
#include <stdio.h>

#define IrPin    0

int main(void)
{
	if(wiringPiSetup() == -1){ //when initialize wiring failed,print messageto screen
		printf("setup wiringPi failed !");
		return 1; 
	}

	pinMode(IrPin, OUTPUT);

	while(1){
		digitalWrite(IrPin, HIGH);
		delay(500);
		digitalWrite(IrPin, LOW);
		delay(500);
	}

	return 0;
}

Python Code

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time

LedPin = 11    # pin11

def setup():
	GPIO.setmode(GPIO.BOARD)       # Numbers GPIOs by physical location
	GPIO.setup(LedPin, GPIO.OUT)   # Set LedPin's mode is output
	GPIO.output(LedPin, GPIO.HIGH) # Set LedPin high(+3.3V) to off led

def loop():
	while True:
		print '...led on'
		GPIO.output(LedPin, GPIO.HIGH)  # led on
		time.sleep(0.5)
		print 'led off...'
		GPIO.output(LedPin, GPIO.LOW) # led off
		time.sleep(0.5)

def destroy():
	GPIO.output(LedPin, GPIO.LOW)     # led off
	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()