Sensors and Displays

RFID RC522

Click here for step by step instructions to build this project!

 

SDA ======> CEO

SCK ========> SCLK

MOSI =======>MOSI

MISO =======> MISO

(skip IRQ)

GND =======> GND

RST ========> GPIO 25

VCC ========> 3.3V

 

16×2 LCD Display

Step 1. Connect pins according to labels on pins

Step 2. Use the following Python code

###########################################################################
#Filename      :i2c1602_lcd.py
#Description   :test i2c 1602 lcd
#Author        :alan
#Website       :www.osoyoo.com
#Update        :2017/07/02
############################################################################
import smbus
import time

# Define some device parameters
I2C_ADDR  = 0x27 # I2C device address, if any error, change this address to 0x27
LCD_WIDTH = 16   # Maximum characters per line

# Define some device constants
LCD_CHR = 1 # Mode - Sending data
LCD_CMD = 0 # Mode - Sending command

LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line
LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line

LCD_BACKLIGHT  = 0x08  # On
#LCD_BACKLIGHT = 0x00  # Off

ENABLE = 0b00000100 # Enable bit

# Timing constants
E_PULSE = 0.0005
E_DELAY = 0.0005

#Open I2C interface
#bus = smbus.SMBus(0)  # Rev 1 Pi uses 0
bus = smbus.SMBus(1) # Rev 2 Pi uses 1

def lcd_init():
  # Initialise display
  lcd_byte(0x33,LCD_CMD) # 110011 Initialise
  lcd_byte(0x32,LCD_CMD) # 110010 Initialise
  lcd_byte(0x06,LCD_CMD) # 000110 Cursor move direction
  lcd_byte(0x0C,LCD_CMD) # 001100 Display On,Cursor Off, Blink Off 
  lcd_byte(0x28,LCD_CMD) # 101000 Data length, number of lines, font size
  lcd_byte(0x01,LCD_CMD) # 000001 Clear display
  time.sleep(E_DELAY)

def lcd_byte(bits, mode):
  # Send byte to data pins
  # bits = the data
  # mode = 1 for data
  #        0 for command

  bits_high = mode | (bits & 0xF0) | LCD_BACKLIGHT
  bits_low = mode | ((bits<<4) & 0xF0) | LCD_BACKLIGHT

  # High bits
  bus.write_byte(I2C_ADDR, bits_high)
  lcd_toggle_enable(bits_high)

  # Low bits
  bus.write_byte(I2C_ADDR, bits_low)
  lcd_toggle_enable(bits_low)

def lcd_toggle_enable(bits):
  # Toggle enable
  time.sleep(E_DELAY)
  bus.write_byte(I2C_ADDR, (bits | ENABLE))
  time.sleep(E_PULSE)
  bus.write_byte(I2C_ADDR,(bits & ~ENABLE))
  time.sleep(E_DELAY)

def lcd_string(message,line):
  # Send string to display

  message = message.ljust(LCD_WIDTH," ")

  lcd_byte(line, LCD_CMD)

  for i in range(LCD_WIDTH):
    lcd_byte(ord(message[i]),LCD_CHR)

def main():
  # Main program block

  # Initialise display
  lcd_init()

  while True:

    # Send some test
    lcd_string("Hello world!   <",LCD_LINE_1)
    lcd_string("(from CyberCamp)",LCD_LINE_2)

    time.sleep(3)
  
    # Send some more text
    lcd_string("camp url:",LCD_LINE_1)
    lcd_string("CyberCamp.rocks",LCD_LINE_2)

    time.sleep(3)

if __name__ == '__main__':

  try:
    main()
  except KeyboardInterrupt:
    pass
  finally:
    lcd_byte(0x01, LCD_CMD)

 

KOOKYE Robot car kit documentation

http://kookye.com/2016/11/25/robot-smart-car-sensor-modules-kit-for-arduino-raspberry-pi/

8×8 LED Matrix display

There are detailed instructions for controlling this display here
In short, you will need to execute the following from your command line:

sudo apt-get install python-dev python-setuptools
sudo apt-get install python3-dev python3-setuptools

sudo -H pip3 install -U setuptools
git clone https://github.com/rm-hull/max7219.git
cd max7219
sudo python setup.py install

Realtime Clock (RTC)

Here is a site that will help you get started

Tilt Sensor

VCC = 3.3V
GND = ground
D0 = Pin port

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

TiltPin = 4
Gpin   = 23
Rpin   = 24

def setup():
	GPIO.setmode(GPIO.BCM)       # Numbers GPIOs by physical location
	GPIO.setup(Gpin, GPIO.OUT)     # Set Green Led Pin mode to output
	GPIO.setup(Rpin, GPIO.OUT)     # Set Red Led Pin mode to output
	GPIO.setup(TiltPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)    # Set BtnPin's mode is input, and pull up to high level(3.3V)
	GPIO.add_event_detect(TiltPin, GPIO.BOTH, callback=detect, bouncetime=200)

def Led(x):
	if x == 0:
		GPIO.output(Rpin, 1)
		GPIO.output(Gpin, 0)
	if x == 1:
		GPIO.output(Rpin, 0)
		GPIO.output(Gpin, 1)

def Print(x):
	if x == 0:
		print '    *************'
		print '    *   Tilt!   *'
		print '    *************'

def detect(chn):
	Led(GPIO.input(TiltPin))
	Print(GPIO.input(TiltPin))

def loop():
	while True:
		pass

def destroy():
	GPIO.output(Gpin, GPIO.HIGH)       # Green led off
	GPIO.output(Rpin, GPIO.HIGH)       # Red 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()




Vibration Sensor SW-420

Instructable for Vibration Sensor

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

#GPIO SETUP
channel = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel, GPIO.IN)

def callback(channel):
        if GPIO.input(channel):
                print "Movement Detected!"
        else:
                print "Movement Detected!"

GPIO.add_event_detect(channel, GPIO.BOTH, bouncetime=300)  # let us know when the pin goes HIGH or LOW
GPIO.add_event_callback(channel, callback)  # assign function to GPIO PIN, Run function on change

# infinite loop
while True:
        time.sleep(1)


Ultrasonic distance sensor

HC-SR04


Documentation





#Libraries
import RPi.GPIO as GPIO
import time
 
#GPIO Mode (BOARD / BCM)
GPIO.setmode(GPIO.BCM)
 
#set GPIO Pins
GPIO_TRIGGER = 23
GPIO_ECHO = 24
 
#set GPIO direction (IN / OUT)
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
 
def distance():
    # set Trigger to HIGH
    GPIO.output(GPIO_TRIGGER, True)
 
    # set Trigger after 0.01ms to LOW
    time.sleep(0.00001)
    GPIO.output(GPIO_TRIGGER, False)
 
    StartTime = time.time()
    StopTime = time.time()
 
    # save StartTime
    while GPIO.input(GPIO_ECHO) == 0:
        StartTime = time.time()
 
    # save time of arrival
    while GPIO.input(GPIO_ECHO) == 1:
        StopTime = time.time()
 
    # time difference between start and arrival
    TimeElapsed = StopTime - StartTime
    # multiply with the sonic speed (34300 cm/s)
    # and divide by 2, because there and back
    distance = (TimeElapsed * 34300) / 2
 
    return distance
 
if __name__ == '__main__':
    try:
        while True:
            dist = distance()
            print ("Measured Distance = %.1f cm" % dist)
            time.sleep(1)
 
        # Reset by pressing CTRL + C
    except KeyboardInterrupt:
        print("Measurement stopped by User")
        GPIO.cleanup()
 


HC-SR501 Infrared PIR Motion Sensor Module


HC-SR501 Infrared PIR Motion Sensor Module
https://www.youtube.com/watch?v=dQY6hNA53oM

Helpful link with full instructions

Project Description

import RPi.GPIO as GPIO
import time

M_pin = 18 #select the pin for motionsensor
B_pin = 26 #select the pin for buzzer

def init():
         GPIO.setwarnings(False)
         GPIO.setmode(GPIO.BCM)
         GPIO.setup(M_pin,GPIO.IN)
         GPIO.setup(B_pin,GPIO.OUT)
         pass

def buzzer():
         while GPIO.input(M_pin):
                  GPIO.output(B_pin,GPIO.LOW)
                  time.sleep(0.5)
                  GPIO.output(B_pin,GPIO.HIGH)
                  time.sleep(0.5)

def detct():
         for i in range(101):
                  if GPIO.input(M_pin):
                           print "Someone is closing!"
                           buzzer()
                  else:
                           GPIO.output(B_pin,GPIO.HIGH)
                           print "Nobody!"
                  time.sleep(2)

time.sleep(5)
init()
detct()

GPIO.cleanup()



DHT11 – Temperature & Humidity Sensor Module
http://www.circuitbasics.com/how-to-set-up-the-dht11-humidity-sensor-on-the-raspberry-pi/

VCC = 3.3V
GND = ground
DATA or Signal = Raspberry Pi GPIO port 4

330 ohm resistor between VCC and DATA

Check this page to load the necessary drivers for this project

NOTE: Although the instructions say to change to the Adafruit_Python_DHT directory to try the example, if you do that you will inadvertently load an old version of the library!! Instead, run the example from OUTSIDE the Adafruit_Python_DHT directory.