Preface About SunFounder SunFounder is a technology company focused on Raspberry Pi and Arduino open source community development. Committed to the promotion of open source culture, we strive to bring the fun of electronics making to people all around the world and enable everyone to be a maker. Our products include learning kits, development boards, robots, sensor modules and development tools. In addition to high quality products, SunFounder also offers video tutorials to help you build your own project.
Contents Components List .................................................................................................................................. 1 Notice .................................................................................................................................................. 13 Introduction to Pin Number of Raspberry Pi................................................................................... 18 Get Source Code ....................................................
Lesson 22 Gas Sensor ...................................................................................................................... 108 Lesson 23 IR Remote Control.......................................................................................................... 112 Lesson 24 Touch Switch................................................................................................................... 117 Lesson 25 Ultrasonic Ranging Module ....................................................
Components List No. Name Qty.
Laser Emitter 1 6 Button 1 7 Tilt-Switch 1 8 Vibration Switch 1 2
9 IR Receiver 1 10 Active Buzzer 1 11 Passive Buzzer 1 12 Reed Switch 1 3
13 Photo-interrupter 1 AD/DA Converter 14 1 PCF8591 15 Raindrop Sensor 1 16 Joystick PS2 1 4
17 Potentiometer 1 18 Analog Hall Sensor 1 19 Hall Switch Sensor 1 20 Analog Temperature Sensor 1 5
21 Thermistor 1 22 Sound Sensor 1 23 Photoresistor 1 24 Flame Sensor 1 6
25 Gas Sensor 1 26 Remote Control 1 27 Touch Switch 1 28 Ultrasonic 1 7
29 Temperature Sensor DS18B20 1 30 Rotary Encoder 1 31 Humiture Sensor 1 32 IR Obstacle Module 1 8
33 I2C LCD1602 Module 1 34 Barometer-BMP180 1 35 MPU6050 Module 1 36 RTC-DS1302 Module 1 9
37 Tracking Sensor 38 Breadboard 1 39 T-Cobbler 1 40 40-pin Ribbon Cable for TCobbler 1 1 10
41 2-Pin Anti-reverse Cable 2 42 3-Pin Anti-reverse Cable 5 43 4-Pin Anti-reverse Cable 5 44 5-Pin Anti-reverse Cable 5 11
2-Pin Ribbon Cable 1 45 (Female to Female) Jumper wires 46 20 (Male to Female) Jumper wires 47 10 (Male to Male) 12
Notice We can easily lead out pins of the Raspberry Pi to breadboard by GPIO Extension Board to avoid GPIO damage caused by frequent plugging in or out. This is our 40-pin GPIO Extension Board and GPIO cable for Raspberry Pi model B+ and Raspberry Pi 2 model B.
Connect the GPIO cable to the Raspberry Pi B+ like this: 14
After connection, it is shown as follows: This is our 26-pin GPIO Extension Board and GPIO cable for Raspberry Pi B.
Connect the GPIO cable to the Raspberry Pi B like this: 16
After connection, it is shown as follows: 17
Introduction to Pin Number of Raspberry Pi Currently, there are three methods of pin numbering for Raspberry Pi. 1. Numbering based on the physical location of pins 2. Numbering appointed by C language GPIO library wiringPi 3.
If we want to operate Raspberry Pi GPIOs in C language based on the wiringPi library, choose numbering appointed by wiringPi. You can see from the above diagram that GPIO0 in wiringPi corresponds to pin 11 numbered by physical location, and GPIO30, to pin 27. The picture below demonstrates the physical numbers of the three pins 11, 12, and 13 in detail: If you are a C user, please install wiringPi.
Press Enter, with the script build, the source code of wiringPi will be compiled automatically and installed to the appropriate directory of Raspberry Pi OS. Step 3: Test whether wiringPi is installed successfully or not WiringPi includes many GPIO commands which enable you to control all kinds of interfaces on Raspberry Pi. You can test whether the wiringPi library is installed successfully or not by the following instructions. gpio –v If the message above appears, the wiringPi is installed successfully.
RPi.GPIO Introduction RPi.GPIO is a module to control Raspberry Pi GPIO channels. This package provides a class to control the GPIO on a Raspberry Pi. For examples and documents, visit http://sourceforge.net/p/raspberry-gpio-python/wiki/Home/ Raspbian OS image of Raspberry Pi installs RPi.GPIO by default, so you can use it directly. You might want to install the python-dev package. sudo apt-get install python-dev Next, use C and Python language to program. You will learn how to use wiringPi and RPi.
Step 2: Use nano or other code edit tools to open led.c, write down the following code and save it. #include #include #define LEDPin 0 int main(void) { if(wiringPiSetup() == -1){//when the wiringPi initialization fails, print message to the screen. printf("setup wiringPi failed !"); return 1; } pinMode(LEDPin, OUTPUT); while(1){ digitalWrite(LEDPin, LOW); //led on printf("led on...\n"); delay(500); digitalWrite(LEDPin, HIGH); //led off printf("...led off\n"); delay(500); } retu
LEDPin = 11 # pin11 GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location GPIO.setup(LEDPin, GPIO.OUT) # Set LEDPin's mode as output GPIO.output(LEDPin, GPIO.HIGH) # Set LEDPin as high(+3.3V) to turn off the led try: while True: print '...led on' GPIO.output(LEDPin, GPIO.LOW) # led on time.sleep(0.5) print 'led off...' GPIO.output(LEDPin, GPIO.HIGH) # led off time.sleep(0.5) except KeyboardInterrupt: be # When 'Ctrl+C' is pressed, the following code will executed. GPIO.output(LEDPin, GPIO.
Get Source Code Method 1: From Github. Change directory to /home/pi cd /home/pi/ Then clone the repository from github git clone https://github.com/sunfounder/SunFounder_SensorKit_for_RPi2.git Method 2: From our website Download the source code from our website. www.sunfounder.com. Click LEARN->Get tutorials, Find and click on Sensor Kit v2.0 for B+. Then on the page of the kit, click the package Sensor_Kit_V2.0_fo_B_RPi2_and_RPi3.zip to download.
Lesson 1 Dual-Color LED Introduction A dual-color light emitting diode (LED) is capable of emitting two different colors of light, typically red and green, rather than only one color. It is housed in a 3mm or 5mm epoxy package. It has 3 leads; common cathode or common anode is available. A dual-color LED features two LED terminals, or pins, arranged in the circuit in anti-parallel and connected by a cathode/anode.
Experimental Procedures Step 1: Build the circuit Raspberry Pi T-Cobbler Dual-Color LED Module GPIO0 GPIO17 R GND GND GND GPIO1 GPIO18 G For C language users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/01_dule_color_led/ Step 3: Compile gcc dule_color_led.c –lwiringPi -lpthread Step 4: Run sudo ./a.
For Python users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/Python/ Step 3: Run sudo python 01_dule_color_led.py You can see the dual-color LED render green, red, and mixed colors.
Lesson 2 RGB LED Module Introduction RGB LED modules can emit various colors of light. Three LEDs of red, green, and blue are packaged into a transparent or semitransparent plastic shell with four pins led out. The three primary colors of red, green, and blue can be mixed and compose all kinds of colors by brightness, so you can make an RGB LED emit colorful light by controlling the circuit.
We can see from the top oscillogram that the amplitude of DC voltage output is 5V. However, the actual voltage output is only 3.75V through PWM, for the high level only takes up 75% of the total voltage within a period. Here are the three basic parameters of PWM: 1. The term duty cycle describes the proportion of "on" time to the regular interval or "period" of time 2. Period describes the reciprocal of pulses in one second. 3. The voltage amplitude here is 0V-5V.
RGB LEDs can be categorized into common anode LED and common cathode LED. In this experiment, we use a common cathode RGB LED.
For C language users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/02_rgb_led/ Step 3: Compile gcc rgb_led.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 02_rgb_led.py You will see the RGB LED light up, and display different colors in turn.
Lesson 3 7-Color Auto-flash LED Introduction On the 7-Color Auto-flash LED module, the LED can automatically flash built-in colors after power on. It can be used to make quite fascinating light effects.
Note: There are two “GND” pins on the module. You only need to connect one of them. Now, you will see 7-color auto-flash LED flashing seven colors.
Lesson 4 Relay Module Introduction Relay is a device which is used to provide connection between two or more points or devices in response to the input signal applied. It is suitable for driving high power electric equipment, such as light bulbs, electric fans and air conditioning. You can use a relay to control high voltage with low voltage by connecting it to Raspberry Pi.
Working of Relay The working principle of relay is simple. When power is supplied to the relay, currents start flowing through the control coil; as a result, the electromagnet starts energizing. Then the armature is attracted to the coil, pulling down the moving contact together thus connecting with the normally open contacts. So the circuit with the load is energized.
The schematic diagram Experimental Procedures Step 1: Build the circuit Raspberry Pi T-Cobbler Relay Module GPIO0 GPIO17 SIG 5V 5V0 VCC GND GND GND 3V3 3V3 COM Dual-color LED Module T-Cobbler Relay Module R * Normal Open GND GND * G * Normal Close 36
For C language users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/04_relay/ Step 3: Compile gcc relay.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 04_relay.
Now, you may hear the ticktock. That's the normally closed contact opened and the normally open contact closed. You can attach a high voltage device you want to control, like a 220V bulb, to the output port of the relay. Then the relay will act as an automatic switch.
Lesson 5 Laser Emitter Module Introduction Laser is widely used in medical treatment, military, and other fields due to its good directivity and energy concentration.
Experimental Procedures Step 1: Build the circuit Raspberry Pi T-Cobbler Laser Emitter Module 5V 5V0 VCC GPIO0 GPIO17 SIG For C language users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/05_laser/ Step 3: Compile gcc laser.
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 05_laser.py Now you can see the module send out Morse signals. Note: Do NOT look directly at the laser head. It can cause great harm to your eyes. You can point the laser beam to the table and see the light spot flashing on the table.
Lesson 6 Button Module Introduction In this lesson, we will use button module to control a dual-color LED module. 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 * Button module - 1 * Dual-color LED module - 2 * 3-Pin anti-reverse cable Experimental Principle Use a normally open button as an input device of Raspberry Pi.
Experimental Procedures Step 1: Build the circuit Raspberry Pi T-Cobbler Button Module GPIO0 GPIO17 SIG 5V 5V0 VCC GND GND GND Raspberry Pi T-Cobbler Dual-Color LED Module GPIO1 GPIO18 R GND GND GND GPIO2 GPIO27 G 43
For C language users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/06_button/ Step 3: Compile gcc button.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 06_button.py The LED on the module will emit green light. If you press the button, "Button pressed" will be printed on the screen and the LED will emit red light.
Lesson 7 Tilt-Switch Module Introduction The tilt-switch module (as shown below) in this kit is a ball tilt-switch with a metal ball inside. It is used to detect inclinations of a small angle. 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 * Dual-color LED module - 1 * Tilt-switch module - 2 * 3-Pin anti-reverse cable Experimental Principle The principle is very simple.
Experimental Procedures Step 1: Build the circuit Raspberry Pi T-Cobbler Tilt Switch Module GPIO0 GPIO17 SIG 5V 5V0 VCC GND GND GND Raspberry Pi T-Cobbler Dual-Color LED Module GPIO1 GPIO18 R GND GND GND GPIO2 GPIO27 G 46
For C language users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/07_tilt_switch/ Step 3: Compile gcc tilt_switch.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 07_tilt_switch.py Place the tilt switch module horizontally, and the LED will flash green. If you tilt it, "Tilt!" will be printed on the screen and the LED will change to red.
Lesson 8 Vibration Switch Introduction A vibration switch, also called spring switch or shock sensor, is an electronic switch which induces shock force and transfers the result to a circuit device thus triggering it to work. It contains the following parts: conductive vibration spring, switch body, trigger pin, and packaging agent.
The schematic diagram: Experimental Procedures Step 1: Build the circuit Raspberry Pi T-Cobbler Vibration Switch Module GPIO0 GPIO17 SIG 5V 5V0 VCC GND GND GND Raspberry Pi T-Cobbler Dual-Color LED Module GPIO1 GPIO18 R GND GND GND GPIO2 GPIO27 G 49
For C language users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/08_vibration_switch/ Step 3: Compile gcc vibration_switch.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 08_vibration_switch.
Now tap or knock the module and you can see the dual-color LED flash red. Tap the sensor again, and the LED will change to green. Each tap or knock would make it change between red and green.
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.
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.
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 Press any key of the remote. Then you can see the LED on the module blinking, and "Received infrared. cnt = xxx" printed on the screen. ""xxx" means the time you pressed the key(s).
Lesson 10 Buzzer Module Introduction Buzzers can be categorized as active and passive ones (See the following picture).
The schematic diagram: Experimental Procedures Active Buzzer Note: The active buzzer has built-in oscillating source, so it will beep as long as it is wired up, but it can only beep with fixed frequency.
For C language users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/10_active_buzzer/ Step 3: Compile gcc active_buzzer.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 10_active_buzzer.
Now you can hear the active buzzer beeping.
For C language users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/10_passive_buzzer/ Step 3: Compile gcc passive_buzzer.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 10_passive_buzzer.
Now you can hear the passive buzzer playing music.
Lesson 11 Reed Switch Introduction A reed switch (as shown below) is used to detect the magnetic field. Hall sensors are generally used to measure the speed of intelligent vehicles and count in assembly lines, while reed switches are often used to detect the existence of a magnetic field.
The schematic diagram: Experimental Procedures Step 1: Build the circuit Raspberry Pi T-Cobbler Reed Switch Module GPIO0 GPIO17 SIG 5V 5V0 VCC GND GND GND Raspberry Pi T-Cobbler Dual-color LED Module GPIO1 GPIO18 R GND GND GND GPIO2 GPIO27 G 62
For C language users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/11_reed_switch/ Step 3: Compile gcc reed_switch.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 11_reed_switch.
Then the LED will flash green. Place a magnet near the reed switch, "Detected Magnetic Material!" will be printed on the screen and the LED will change to red. Move away the magnet, the LED will turn green again.
Lesson 12 Photo-interrupter Introduction A photo-interrupter (as shown below) is a sensor with a light-emitting component and lightreceiving component packaged and placed on face-to-face. It applies the principle that light is interrupted when an object passes through the sensor. Therefore, photo-interrupters are widely used in speed measurement.
Experimental Procedures Step 1: Build the circuit Raspberry Pi T-Cobbler Photo-interrupter Module GPIO0 GPIO17 SIG 5V 5V0 VCC GND GND GND Raspberry Pi T-Cobbler Dual-color LED Module GPIO1 GPIO18 R GND GND GND GPIO2 GPIO27 G 66
For C language users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/12_photo_interrupter/ Step 3: Compile gcc photo_interrupter.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 12_photo_interrupter.py Now the LED will light up green. Stick a piece of paper in the gap of photo interrupter. Then "Light was blocked" will be printed on the screen and the LED will flash red.
Lesson 13 PCF8591 Introduction The PCF8591 is a single-chip, single-supply low-power 8-bit CMOS data acquisition device with four analog inputs, one analog output and a serial I2C-bus interface. Three address pins A0, A1 and A2 are used for programming the hardware address, allowing the use of up to eight devices connected to the I2C-bus without additional hardware. Address, control and data to and from the device are transferred serially via the two-line bidirectional I2C-bus.
Control byte: The second byte sent to a PCF8591 device will be stored in its control register and is required to control the device function. The upper nibble of the control register is used for enabling the analog output, and for programming the analog inputs as single-ended or differential inputs. The lower nibble selects one of the analog input channels defined by the upper nibble (see Fig.5). If the auto-increment flag is set, the channel number is incremented automatically after each A/D conversion.
In this experiment, the AIN0 (Analog Input 0) port is used to receive analog signals from the potentiometer module, and AOUT (Analog Output) is used to output analog signals to the dual-color LED module so as to change the luminance of the LED.
Step 2: Setup I2C (see Appendix 1. If you have set I2C, skip this step.) For C language users: Step 3: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/13_pcf8591/ Step 4: Compile gcc pcf8591.c -lwiringPi Step 5: Run sudo ./a.out For Python users: Step 3: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/Python/ Step 4: Run sudo python 13_pcf8591.
Now, turn the knob of the potentiometer on PCF8591, and you can see the luminance of the LED change and a value between 0 and 255 printed on the screen.
Lesson 14 Rain Detection Module Introduction The rain detection module detects rain on the board. Place the rain detection board in the open air. When it is raining, the rain detection module will sense the raindrops and send signals to the Raspberry Pi.
The schematic diagram: Experimental Procedures Step 1: Build the circuit Raspberry Pi T-Cobbler PCF8591 Module SDA SDA1 SDA SCL SCL1 SCL 3V3 3V3 VCC GND GND GND LM393 T-Cobbler PCF8591 Module DO GPIO17 * AO * AIN0 VCC 3V3 VCC GND GND GND Rain Detection Board LM393 - IN - GND Note: The two pins on the rain detection board are exactly the same. You can connect them to pin IN and GND on LM393.
For C language users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/14_rain_detector/ Step 3: Compile gcc rain_detector.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 14_rain_detector.
Now drop some water onto the rain detection board until "raining" displayed on the screen. You can adjust the potentiometer on LM393 to detect the threshold of rainfall.
Lesson 15 Joystick PS2 Introduction There are five operation directions for joystick PS2: up, down, left, right and press-down.
Experimental Procedures Step 1: Build the circuit Raspberry Pi T-Cobbler PCF8591 Module SDA SDA1 SDA SCL SCL1 SCL 3.
For C language users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/15_joystick_PS2/ Step 3: Compile gcc joystick_PS2.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 15_joystick_PS2.
Lesson 16 Potentiometer Module Introduction A potentiometer is a device which is used to vary the resistance in an electrical circuit without interrupting the circuit.
The schematic diagram: Experimental Procedures Step 1: Build the circuit Raspberry Pi T-Cobbler PCF8591 Module SDA SDA1 SDA SCL SCL1 SCL 3V3 3V3 VCC GND GND GND Potentiometer T-Cobbler PCF8591 Module SIG * AIN0 VCC 3V3 VCC GND GND GND Dual-Color Module T-Cobbler PCF8591 Module R * AOUT GND GND GND G * * 81
For C language users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/16_potentiometer/ Step 3: Compile gcc potentiometer.c -lwiringPi Step 4: Run sudo ./a.
For Python users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/Python/ Step 3: Run sudo python 16_potentiometer.py Turn the knob of the potentiometer, and you can see the value printed on the screen change from 0 (minimum) to 255 (maximum).
Lesson 17 Hall Sensor Introduction Based on Hall Effect, a Hall sensor is a one that varies its output voltage in response to a magnetic field. Hall sensors are used for proximity switching, positioning, speed detection, and current sensing applications. Hall sensors can be categorized into linear (analog) Hall sensors and switch Hall sensors. A switch Hall sensor consists of voltage regulator, Hall element, differential amplifier, Schmitt trigger, and output terminal and it outputs digital values.
seen as a charge separation, with a buildup of either positive or negative charges on the bottom or on the top of the plate. Hall sensor A Hall sensor is a kind of magnetic field sensor based on it. Electricity carried through a conductor will produce a magnetic field that varies with current, and a Hall sensor can be used to measure the current without interrupting the circuit. Typically, the sensor is integrated with a wound core or permanent magnet that surrounds the conductor to be measured.
The schematic diagram of the Switch hall module: Experimental Procedures For switch Hall sensor, take the following steps.
For C language users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/17_switch_hall/ Step 3: Compile gcc switch_hall.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 17_switch_Hall.
Put a magnet close to the Switch Hall sensor. Then a string “Detected magnetic materials” will be printed on the screen and the LED will light up.
For C language users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/17_analog_hall_switch/ Step 3: Compile gcc analog_hall_switch.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 17_analog_hall_switch.
Now "Current intensity of magnetic field : xxx " will be displayed on the screen. Put the magnet close to the analog Hall sensor, with the north magnetic pole towards the sensor, and then " Magnet: North." will be displayed. Move the magnet away, and " Magnet: None." will be printed. If the magnet approaches the sensor with the south magnetic pole towards it, " Magnet: South." will be printed on the screen.
Lesson 18 Temperature Sensor Introduction A temperature sensor is a component that senses temperature and converts it into output signals. By material and component features, temperature sensors can be divided into two types: thermal resistor and thermocouple. Thermistor is one kind of the former type. It is made of semiconductor materials; most thermistors are negative temperature coefficient (NTC) ones, the resistance of which decreases with rising temperature.
The schematic diagram for analog temperature sensor: The schematic diagram for the thermistor module: Experimental Procedures Step 1: Build the circuit Raspberry Pi T-Cobbler PCF8591 Module SDA SDA1 SDA SCL SCL1 SCL 3V3 3V3 VCC GND GND GND 92
For thermistor module: Thermistor Module T-Cobbler PCF8591 Module SIG * AIN0 VCC 3V3 VCC GND GND GND 93
For analog temperature sensor module: Analog Temperature Module T-Cobbler PCF8591 Module DO GPIO17 * AO * AIN0 VCC 3V3 VCC GND GND GND For C language users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/18_thermistor/ Step 3: Compile gcc thermistor.c –lwiringPi -lm Step 4: Run sudo ./a.
For Python users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/Python/ Step 3: Run sudo python 18_thermistor.py Now touch the thermistor and you can see the value of current temperature printed on the screen change accordingly. Temperature alarm setting: If you use the Analog Temperature Sensor module, uncomment the line under 1: For C language: 55 // For a threshold, uncomment one of the code for 56 // which module you use.
60 61 // 2. For Thermister module(with sig pin) 62 if (temp > 33) tmp = 0; 63 else if (temp < 31) tmp = 1; 64 //------------------------------------------ For Python 41 ################################################# 42 # 1. For Analog Temperature module(with DO) 43 #tmp = GPIO.input(DO); 44 # 45 # 2.
The physical picture for analog temperature sensor: The physical picture for thermistor module: 97
Lesson 19 Sound Sensor Introduction Sound sensor is a component that receives sound waves and converts them into electrical signal. It detects the sound intensity in ambient environment like a microphone.
The schematic diagram: Experimental Procedures Step 1: Build the circuit according to the following method Raspberry Pi T-Cobbler PCF8591 Module SDA SDA1 SDA SCL SCL1 SCL 3V3 3V3 VCC GND GND GND Sound Sensor Module T-Cobbler PCF8591 Module SIG * AIN0 VCC 3V3 VCC GND GND GND 99
For C language users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/19_sound_sensor/ Step 3: Compile gcc sound_sensor.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 19_sound_sensor.
Now, speak close to or blow to the microphone, and you can see “Voice In!! ***” printed on the screen.
Lesson 20 Photoresistor Module Introduction A photoresistor is a light-controlled variable resistor. The resistance of a photoresistor decreases with increasing incident light intensity.
Experimental Procedures Step 1: Build the circuit Raspberry Pi T-Cobbler PCF8591 Module SDA SDA1 SDA SCL SCL1 SCL 3V3 3V3 VCC GND GND GND Photoresistor T-Cobbler PCF8591 Module SIG * AIN0 VCC 3V3 VCC GND GND GND 103
For C language users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/20_photoresistor/ Step 3: Compile gcc photoresistor.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 20_photoresistor.py Now, change light intensity (e.g. cover the module with a pad), and the value printed on the screen will change accordingly.
Lesson 21 Flame Sensor Introduction A flame sensor (as shown below) performs detection by capturing infrared rays with specific wavelengths from flame. It can be used to detect and warn of flames.
Experimental Procedures Step 1: Build the circuit Raspberry Pi T-Cobbler PCF8591 Module SDA SDA1 SDA SCL SCL1 SCL 3V3 3V3 VCC GND GND GND Flame Sensor T-Cobbler PCF8591 Module DO GPIO17 * AO * AIN0 VCC 3V3 VCC GND GND GND 106
For C language users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/21_flame_sensor/ Step 3: Compile gcc flame_sensor.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 21_flame_sensor.py Now, ignite a lighter near the sensor, within the range of 80cm, and "Fire!" will be displayed on the screen.
Lesson 22 Gas Sensor Introduction Gas Sensor MQ-2 is a sensor for flammable gas and smoke by detecting the concentration of combustible gas in the air. They are used in gas detecting equipment for smoke and flammable gasses in household, industry or automobile.
The schematic diagram: Experimental Procedures Step 1: Build the circuit Raspberry Pi T-Cobbler PCF8591 Module SDA SDA1 SDA SCL SCL1 SCL 3V3 3V3 VCC GND GND GND Gas Sensor Module T-Cobbler PCF8591 Module DO GPIO17 * AO * AIN0 VCC 5V0 VCC GND GND GND Raspberry Pi T-Cobbler Active Buzzer Module GPIO1 GPIO18 SIG 5V 3V3 VCC GND GND GND 109
For C language users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/22_gas_sensor/ Step 3: Compile gcc gas_sensor.c –lwiringPi Step 4: Run sudo ./a.
Step 3: Run sudo python 22_gas_sensor.py Place a lighter close to the MQ-2 gas sensor, and press the switch to release gasses. A value between 0 and 255 will be displayed on the screen. If harmful gases reach a certain concentration, the buzzer will beep, and “Danger Gas!” will be printed on the screen. You can also turn the shaft of the potentiometer on the module to raise or reduce the concentration threshold. The MQ-2 gas sensor needs to be heated up for a while.
Lesson 23 IR Remote Control Introduction Each button of an IR remote control (as shown below) has a string of specific encoding. When a button is pressed, the IR transmitter in the remote control will send out the corresponding IR encoding signals. On the other side, when the IR receiver receives certain encoding signals, it will decode them to identify which button is pressed.
Experimental Procedures Step 1: Build the circuit Raspberry Pi T-Cobbler IR Receiver Module GPIO4 GPIO23 SIG 5V 5V0 VCC GND GND GND Raspberry Pi T-Cobbler RGB LED Module 5V 5V0 VCC GPIO0 GPIO17 R GPIO1 GPIO18 G GPIO2 GPIO27 B 113
Step 2: Download the LIRC library sudo apt-get install lirc Step 3: Set up lirc: Open your /etc/modules file: sudo nano /etc/modules Add this to the end: lirc_dev lirc_rpi gpio_in_pin=23 gpio_out_pin=22 Press Ctrl +O and Ctrl +X, save and exit . Open the /etc/lirc/hardware.conf file: sudo nano /etc/lirc/hardware.conf Modify the file as shown below: # Run "lircd --driver=help" for a list of supported drivers.
Check if lirc module is loaded: ls /dev/li* You should see this: /dev/lirc0 /dev/lircd Run the command to stop lircd and start outputting raw data from the IR receiver: irw When you press a button on the remote, you can see the button name printed on the screen. pi@raspberrypi:~ $ irw 0000000000000001 00 KEY_CHANNELDOWN /home/pi/lircd.conf 0000000000000002 00 KEY_CHANNEL /home/pi/lircd.conf 0000000000000003 00 KEY_CHANNELUP /home/pi/lircd.conf 0000000000000004 00 KEY_PREVIOUS /home/pi/lircd.
Copy the lircrc file to /etc/lirc/lirc/: sudo cp lircrc /etc/lirc/lirc/ Step 8: Compile gcc ircontrol.c -lwiringPi -llirc_client Step 9: Run sudo ./a.out For Python users: Step 5: Download pylirc: sudo apt-get install python-pylirc Step 6: Change directory: cd /home/pi/SunFounder_SensorKit_for_RPi2/Python/ Step 7: Run sudo python 23_ircontrol.py Each of the top three rows of buttons on the remote control represents a kind of color, i.e. red, green, and blue, top to bottom.
Lesson 24 Touch Switch Introduction A touch sensor operate with the conductivity of human body. When you touch the metal on the base electrode of the transistor, the level of pin SIG will turn over.
Experimental Procedures Step 1: Build the circuit Raspberry Pi T-Cobbler Touch Sensor Module GPIO0 GPIO17 SIG 5V 5V0 VCC GND GND GND Raspberry Pi T-Cobbler Dual-Color LED Module GPIO1 GPIO18 R GND GND GND GPIO2 GPIO27 G 118
For C language users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/24_touch_switch/ Step 3: Compile gcc touch_switch.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 24_touch_switch.py Now, touch the metal disk, you can see the LED change its colors and "ON" and "OFF" printed on the screen.
Lesson 25 Ultrasonic Ranging Module Introduction The ultrasonic sensor uses sound to accurately detect objects and measure distances. It sends out ultrasonic waves and converts them into electronic signals.
For C language users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/25_ultrasonic_ranging/ Step 3: Compile gcc ultrasonic_ranging.c -lwiringPi Step 4: Run sudo ./a.
Step 3: Run sudo python 25_ultrasonic_ranging.py Now you can see the distance between the ultrasonic ranging module and the obstacle (like your palm) in front on the screen. Sway your hand over the ultrasonic ranging module slowly and observe the distance printed on the screen.
Lesson 26 DS18B20 Temperature Sensor Introduction Temperature Sensor DS18B20 is a commonly used digital temperature sensor featured with small size, low-cost hardware, strong anti-interference capability and high precision. The digital temperature sensor is easy to wire and can be applied a various occasions after packaging. Different from conventional AD collection temperature sensors, it uses a 1-wire bus and can directly output temperature data.
Experimental Procedures Step 1:Build the circuit according to the following method Raspberry Pi T-Cobbler DS18B20 Temperature Sensor GPIO7 GPIO4 SIG 5V 5V0 VCC GND GND GND Step 2:Upgrade your kernel sudo apt-get update sudo apt-get upgrade Step 3:You can edit that file with nano sudo nano /boot/config.
Then reboot with sudo reboot. Mount the device drivers and confirm whether the device is effective or not sudo modprobe w1-gpio sudo modprobe w1-therm cd /sys/bus/w1/devices/ ls The result is as follows: root@rasberrypi:/sys/bus/w1/devices# ls 28-00000495db35 w1_bus_master1 28-00000495db35 is an external temperature sensor device, but it may vary with every client. This is the serial number of your ds18b20.
Step 7: Run sudo ./a.out For Python users: Step 5: Change directory and edit cd /home/pi/SunFounder_SensorKit_for_RPi2/Python/ nano 26_ds18b20.py Step 6: Run sudo python 26_ds18b20.py Now, you can see the current temperature value displayed on the screen.
Lesson 27 Rotary Encoder Module Introduction A rotary encoder is an electro-mechanical device that converts the angular position or motion of a shaft or axle to analog or digital code. Rotary encoders are usually placed at the side which is perpendicular to the shaft. They act as sensors for detecting angle, speed, length, position, and acceleration in automation field.
It shows that if output 1 is high and output 2 is high, then the switch rotates clockwise; if output 1 is high and output 2 is low, then the switch rotates counterclockwise. As a result, during SCM programming, if output 1 is high, then you can tell whether the rotary encoder rotates left or right as long as you know the state of output 2.
For C language users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/27_rotary_encoder/ Step 3: Compile gcc rotary_encoder.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 27_rotary_encoder.
Now rotate the shaft of the rotary encoder, and the value printed on the screen will change. Rotate the rotary encoder clockwise, the value will increase; Rotate it counterclockwise, the value will decrease; Press the rotary encoder, the value will be reset to 0.
Lesson 28 Humiture Sensor Introduction The digital temperature and humidity sensor DHT11 is a composite sensor that contains a calibrated digital signal output of temperature and humidity. The technology of a dedicated digital modules collection and the temperature and humidity sensing technology are applied to ensure that the product has high reliability and excellent long-term stability.
Only three pins are available for use: VCC, GND, and DATA. The communication process begins with the DATA line sending start signal to DHT11, and DHT11 receives the signal and returns an answer signal, then the host receives the answer signal and begins to receive 40bit humiture data (8-bit humidity integer + 8-bit humidity decimal + 8-bit temperature integer + 8-bit temperature decimal + 8-bit checksum). For more information, please refer to the datasheet of DHT11.
Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/28_humiture/ Step 3: Compile gcc humiture.c -lwiringPi Step 4: Run sudo ./a.out For Python users: cd /home/pi/SunFounder_SensorKit_for_RPi2/Python/SunFounder_DHT11/ Step 3:Update submodule SunFounder_DHT11 git submodule update --init Step 4: Run sudo python dht11.py Now, you can see humidity and temperature value printed on the screen.
134
Lesson 29 IR Obstacle Avoidance Module Introduction An IR obstacle avoidance module (as shown below) uses infrared reflection principle to detect obstacles. When there is no object ahead, infrared-receiver cannot receive signals; when there is an object ahead, it will block and reflect infrared light, then infrared-receiver can receive signals.
The schematic diagram: Experimental Procedures Step 1: Build the circuit Raspberry Pi T-Cobbler IR Obstacle Module GPIO0 GPIO17 SIG 5V 5V0 VCC GND GND GND 136
For C language users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/30_ir_obstacle/ Step 3: Compile gcc ir_obstacle.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 30_ir_obstacle.
Now, if there is an obstacle ahead, a string “Detected Barrier!” will be printed on the screen.
Lesson 30 I2C LCD1602 Introduction LCD1602 is a character type liquid crystal display, which can display 32 (16*2) characters at the same time. It has 16 pins, of which at least 7 would be used each time. You can use a PCF8574 I2C chip to expand I/O ports so only two GPIO ports would be occupied.
Step 2: Setup I2C (see Appendix 1. If you have set I2C, skip this step.) For C language users: Step 3: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/30_i2c_lcd1602/ Step 4: Compile gcc i2c_lcd1602.c -lwiringPi Step 5: Run sudo ./a.
Step 4: Run sudo python 30_i2c_lcd1602.py Now you can see “Greetings! From SunFounder” displayed on the LCD.
Lesson 31 Barometer Introduction The BMP180 barometer is the new digital barometric pressure sensor, with a very high performance, which enables applications in advanced mobile devices, such as smart phones, tablets and sports devices. It complies with the BMP085 but boasts many improvements, like a smaller size and more digital interfaces.
Experimental Procedures Step 1: Build the circuit Raspberry Pi T-Cobbler Barometer Module SCL SCL1 SCL SDA SDA1 SDA 5V 5V0 VCC GND GND GND Step2: Setup I2C (see Appendix 1. If you have set I2C, skip this step.
Step 5: Compile gcc barometer.c bmp180.c -lm Step 6: Run sudo ./a.out For Python users: Step 3: Install smbus for I2C sudo apt-get install python-smbus i2c-tools Step 4: We'll need to install some utilities for the Raspberry Pi to communicate over I2C. sudo apt-get install build-essential python-dev python-smbus cd ~ git clone https://github.com/adafruit/Adafruit_Python_BMP.git cd Adafruit_Python_BMP sudo python setup.
Lesson 32 MPU6050 Gyro Acceleration Sensor Introduction The MPU-6050 is the world’s first and only 6-axis motion tracking devices designed for the low power, low cost, and high performance requirements of smartphones, tablets and wearable sensors.
Step2: Setup I2C (see Appendix 1. If you have set I2C, skip this step.) For C language users: Step 3: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/32_mpu6050/ Step 4: Compile gcc 32_mpu6050_accel.c –lwiringPi –lm gcc 32_mpu6050_gyro.c –lwiringPi –lm Step 5: Run sudo ./a.
Step 4: Run sudo python 32_mpu6050.py Now you can see the values of the acceleration sensor, gyroscope, and XY-axis rotation read by MPU6050 printed on the screen constantly.
Lesson 33 RTC DS1302 Introduction DS1302 is a trickle charging clock chip, launched by DALLAS in America. With a built-in realtime clock/calendar and a 31-byte static RAM, it can communicate with MCU through simple serial interfaces. The real-time clock/calendar circuit provides information about second, minute, hour, day, week, month, and year. DS1302 can automatically adjust the number of days per month and days in leap year. You can determine to use a 24-hour or 12hour system by AM/PM selection.
The schematic diagram: Experimental Procedures For C language users: Step 1: Build the circuit Raspberry Pi T-Cobbler RTC DS1302 Module GPIO0 GPIO17 SCL GPIO1 GPIO18 SDA GPIO2 GPIO27 RST 5V 5V0 VCC GND GND GND 149
Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/33_ds1302/ Step 3: Compile: gcc rtc_ds1302.c -lwiringPi -lwiringPiDev Step 4: Set up time by: sudo ./a.out –sdsc Set year, month, date as YYMMDD Set hour, minute, second as HHMMSS(24-hour clock) Set weekday (0 as Sunday) Step 5: Run: sudo ./a.
For Python users: Step 1: Build the circuit Raspberry Pi T-Cobbler RTC DS1302 Module GPIO4 GPIO23 SCL GPIO5 GPIO24 SDA GPIO6 GPIO25 RST 5V 5V0 VCC GND GND GND Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/Python/ Step 3: Run sudo python 33_ds1302.
Now you can see the time on the screen.
Lesson 34 Tracking Sensor Introduction The infrared tracking sensor uses a TRT5000 sensor. The blue LED of TRT5000 is the emission tube and after electrified it emits infrared light invisible to human eye. The black part of the sensor is for receiving; the resistance of the resistor inside changes with the infrared light received.
Experimental Procedures Step 1: Build the circuit Raspberry Pi T-Cobbler Tracking Sensor Module GPIO0 GPIO17 SIG 5V 5V0 VCC GND GND GND 154
For C language users: Step 2: Change directory cd /home/pi/SunFounder_SensorKit_for_RPi2/C/34_tracking/ Step 3: Compile gcc tracking.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 34_tracking.py When the tracking sensor encounters black lines, a string “Black Line is detected” will be printed on the screen.
Lesson 35 Intelligent Temperature Measurement System Introduction In this experiment, we will use some modules together to build an intelligent temperature measurement system.
Raspberry Pi T-Cobbler PCF8591 Module SDA SDA1 SDA SCL SCL1 SCL 3V3 3V3 VCC GND GND GND Joystick PS2 T-Cobbler PCF8591 Module Y * AIN0 X * AIN1 Bt * AIN2 VCC 3V3 * GND GND * Raspberry Pi T-Cobbler RGB LED Module GPIO0 GPIO17 R GPIO1 GPIO18 G GPIO2 GPIO27 B 5V 5V0 VCC Raspberry Pi T-Cobbler Active Buzzer Module GPIO3 GPIO22 SIG 3V 3V3 VCC GND GND GND 157
For C language users: Step 2: Check the address of your sensor ls /sys/bus/w1/devices/ It may be like this: 28-031467805fff w1_bus_master1 Copy or write down 28-XXXXXXX. It is the address of your sensor. Step 2: Change directory and edit cd /home/pi/SunFounder_SensorKit_for_RPi2/C/35_expand02/ nano temp_monitor.
Find the function float tempRead(void), and the line "fd = open(XXXXXX)". Replace "28031467805ff" with your sensor address. float tempRead(void) { float temp; int i, j; int fd; int ret; char buf[BUFSIZE]; char tempBuf[5]; fd = open("/sys/bus/w1/devices/28-031467805fff/w1_slave", O_RDONLY); if(-1 == fd){ perror("open device file error"); return 1; } Save and exit. Step 4: Compile gcc temp_monitor.c -lwiringPi Step 5: Run sudo ./a.
Now, you can pull the shaft of the joystick left and right to set the upper limit value, and up and down to set the lower limit value. Then, if the ambient temperature reaches the upper limit value or lower limit value, the buzzer will beep in a different frequency to warn.
Appendix 1: I2C Configuration Step 1: Enable the I2C port of your Raspberry Pi (If you have enabled it, skip this; if you do not know whether you have done that or not, please continue): sudo raspi-config 8 Advanced options A7 I2C 161
162
163
(If you do not see this page, continue to the next step) Step 2: Check that the i2c modules are loaded and active: lsmod | grep i2c Then the following code will appear (the number may be different) i2c_dev 6276 0 i2c_bcm2708 4121 0 Step 3: Install i2c-tools sudo apt-get install i2c-tools Step 4: Check the address of the I2C device: i2cdetect -y 1 # For Raspberry Pi 2 i2cdetect -y 0 # For Raspberry Pi 1 pi@raspberrypi ~ $ i2cdetect -y 1 0 00: 1 2 3 4 5 6 7 8 9 a b c d e f --
If there's an I2C device connected, the results will be similar as shown above – since the address of the device is 0x48, 48 is printed. Step 5: For C language users: Install libi2c-dev sudo apt-get install libi2c-dev For Python users: Install smbus for I2C sudo apt-get install python-smbus Copyright Notice All contents including but not limited to texts, images, and code in this manual are owned by the SunFounder Company.