RPi 23 I 2 C Analog Sensor jyheo

  • Slides: 12
Download presentation
RPi 2/3, I 2 C, Analog Sensor jyheo 0@gmail. com

RPi 2/3, I 2 C, Analog Sensor jyheo 0@gmail. com

I 2 C (I-squared-C) ● Serial computer bus invented by Philips Semiconductor ● Typically

I 2 C (I-squared-C) ● Serial computer bus invented by Philips Semiconductor ● Typically used for attaching lower-speed peripheral ICs to processors and microcontrollers A sample schematic with one master (a microcontroller), three slave nodes (an ADC, a DAC, and a microcontroller), and pull-up resistors Rp https: //en. wikipedia. org/wiki/I%C 2%B 2 C

ADC, DAC ● ADC - Analog to Digital Converter ● DAC - Digital to

ADC, DAC ● ADC - Analog to Digital Converter ● DAC - Digital to Analog Converter ● PCF 8591 t ○ ○ 3 ADC, 1 DAC value range: 0~255 P 4 P 5 P 6 ● YL-40 board with PCF 8591 t Jumper P 4 for AIN 1: R 6 thermister Jumper P 5 to AIN 0: R 7 photocell Jumper P 6 to AIN 3: The single turn 10 K ohm resistance Removing a jumper allows an input channel to be fed from one of the external pins, labelled accordingly. SCL SDA GND VCC AOUT AIN 0 AIN 1 AIN 2 AIN 3

Enable I 2 C in RPi ● Enable I 2 C in raspi-config ●

Enable I 2 C in RPi ● Enable I 2 C in raspi-config ● $ sudo raspi-config ● Interfacing Options 선택 > I 2 C 선택

Enable I 2 C in RPi ● Check if the I 2 C kernel

Enable I 2 C in RPi ● Check if the I 2 C kernel module is loaded ○ ○ $ lsmod i 2 c-dev, i 2 c-bcm? ? must be listed! ● Test ○ $ i 2 cdetect -y 1

Wiring RPi and YL-40 RPi SCL 1 SDA 1 GND VCC 3 V 3

Wiring RPi and YL-40 RPi SCL 1 SDA 1 GND VCC 3 V 3

import smbus Photocell on YL-40 ● $ python 3 yl 40. py 0 import

import smbus Photocell on YL-40 ● $ python 3 yl 40. py 0 import time import sys bus = smbus. SMBus(1) address = 0 x 48 if len(sys. argv) == 2: # AIN 0 0 x 40: photocell i = int(sys. argv[1]) # AIN 1 0 x 41: thermister input_addr = 0 x 40 + i # AIN 3 0 x 43: registance else: input_addr = 0 x 40 while True: bus. write_byte(address, input_addr) bus. read_byte(address) aread = bus. read_byte(address) print(aread) https: //github. com/jyheo/rpi 2/blob/master/yl 40. py time. sleep(0. 2)

Temperature Sensor - LM 35 ● Wiring ○ ○ ○ LM 35 1 to

Temperature Sensor - LM 35 ● Wiring ○ ○ ○ LM 35 1 to RPi 3 V 3 OUT to YL-40 AIN 2 GND to RPi GND ● $ python 3 yl 40. py 2 https: //github. com/jyheo/rpi 2/blob/master/yl 40. py

Vibration Sensor ● Wiring ○ ○ One leg of Vibration Sensor to RPi 3

Vibration Sensor ● Wiring ○ ○ One leg of Vibration Sensor to RPi 3 V 3 The other leg to YL-40 AIN 2 ● $ python 3 yl 40. py 2 https: //github. com/jyheo/rpi 2/blob/master/yl 40. py

IR Receiver ● Wiring ○ ○ Longer leg of IR Sensor to RPi 3

IR Receiver ● Wiring ○ ○ Longer leg of IR Sensor to RPi 3 V 3 The other leg to YL-40 AIN 2 ● $ python 3 yl 40. py 2 ● IR Receiver can be used to detect motion https: //github. com/jyheo/rpi 2/blob/master/yl 40. py

Analog OUT ● $ python 3 yl 40 w. py 255 ○ Turn on

Analog OUT ● $ python 3 yl 40 w. py 255 ○ Turn on green LED of YL-40 ● $ python 3 yl 40 w. py 0 ○ Turn off the LED import smbus import time import sys bus = smbus. SMBus(1) address = 0 x 48 if len(sys. argv) == 2: v = int(sys. argv[1]) bus. write_byte_data(address, 0 x 40, v) else: print(‘sudo python yl 40 w. py 0~255’) https: //github. com/jyheo/rpi 2/blob/master/yl 40 w. py

Exercise ● Write a python program ○ To display temperature and light ● Motion

Exercise ● Write a python program ○ To display temperature and light ● Motion detection LED ○ ○ Detect motion using IR receiver Once detecting motion, keep turning LED on for 3 seconds