pigpio on Raspberry pi

Introduction

--This is a memorandum that I researched about ** pigpio ** a long time ago.

Official site

Installation

python


$ sudo apt install pigpio python-pigpio python3-pigpio

start pigpiod

python


$ sudo pigpiod

automatic start of pigpio

python


$ sudo systemctl enable pigpiod.service
$ sudo shutdown -r now

#Confirmation after reboot
$ sudo systemctl status pigpiod.service

First of all, L Chika

led_pigpio.py


from time import sleep
import pigpio

pig = pigpio.pi()
pig.set_mode(21, pigpio.OUTPUT)

try:
    while True:
        sleep(1)
        pig.write(21, 1)
        sleep(1)
        pig.write(21, 0)
except KeyboardInterrupt:
    pig.stop()

Event processing

sw_pd_event.py


from time import sleep
import pigpio

pig = pigpio.pi()
pig.set_mode(4, pigpio.INPUT)
pig.set_pull_up_down(4, pigpio.PUD_DOWN)

def cbf(gpio, level, tick):
    print(gpio, level, tick)

cb = pig.callback(4, pigpio.RISING_EDGE, cbf)

try:
    while True:
        sleep(0.01)
except KeyboardInterrupt:
    pig.stop()

LCD display

lcd_pigpio.y


from time import sleep

import pigpio
from RPLCD.pigpio import CharLCD

pi = pigpio.pi()

lcd = CharLCD(pi, cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[33, 31, 29, 23], pin_contrast=9)
lcd.write_string('Hello RasPi')
sleep(3)
lcd.clear()
lcd.write_string('Test LCD pigpio')
sleep(3)
lcd.close(clear=True)
pi.stop()

i2c: 9-axis sensor module (BMX055)

i2c_pigpio.py


from time import sleep

import pigpio
from BMX055 import bmx055

bmx = bmx055()
bmx.setup()

try:
    while True:
        ax, ay, az = bmx.read_accel()
#        print("accel: ax={0}, ay={1}, az={2}".format(ax, ay, az))

        gx, gy, gz = bmx.read_gyro()
#        print("gyro: gx={0}, gy={1}, gz={2}".format(gx, gy, gz))

        cx, cy, cz = bmx.read_compass()
#        print("comp: cx={0}, cy={1}, cz={2}".format(cx, cy, cz))

        msg = "|       |  x  |  y  |  z  |\n"
        msg += "| accel | "+str(ax)+" | "+str(ay)+" | "+str(az)+" |\n"
        msg += "| gyro  | "+str(gx)+" | "+str(gy)+" | "+str(gz)+" |\n"
        msg += "| comp  | "+str(cx)+" | "+str(cy)+" | "+str(cz)+" |\n"
        print(msg)
        sleep(0.5)
except KeyboardInterrupt:
    bmx.stop()

BMX055.py


from time import sleep

import pigpio

class bmx055:
    def __init__(self):
        self.pi = pigpio.pi() 
        self.i2c_bus = 1 
        self.accel_addr = 0x19 
        self.accel = None 
        self.comp_addr = 0x13
        self.comp = None 
        self.gyro_addr = 0x69
        self.gyro = None 
        
    def open(self, i2c_addr):    
        return self.pi.i2c_open(self.i2c_bus, i2c_addr)

    def close(self, handle):
        self.pi.i2c_close(handle)

    def stop(self):
        self.close(self.accel)
        self.close(self.comp)
        self.close(self.gyro)
        self.pi.stop()

    def setup(self):
        self.accel = self.open(self.accel_addr) 
        self.pi.i2c_write_byte_data(self.accel, 0x0f, 0x03)
        sleep(0.1)
        self.pi.i2c_write_byte_data(self.accel, 0x10, 0x08)
        sleep(0.1)
        self.pi.i2c_write_byte_data(self.accel, 0x11, 0x00)
        sleep(0.1)
        
        self.gyro = self.open(self.gyro_addr) 
        self.pi.i2c_write_byte_data(self.gyro, 0x0f, 0x04)
        sleep(0.1)
        self.pi.i2c_write_byte_data(self.gyro, 0x10, 0x07)
        sleep(0.1)
        self.pi.i2c_write_byte_data(self.gyro, 0x11, 0x00)
        sleep(0.1)

        self.comp = self.open(self.comp_addr) 
        self.pi.i2c_write_byte_data(self.comp, 0x4b, 0x83)
        sleep(0.1)
        self.pi.i2c_write_byte_data(self.comp, 0x4b, 0x01)
        sleep(0.1)
        self.pi.i2c_write_byte_data(self.comp, 0x4c, 0x00)
        self.pi.i2c_write_byte_data(self.comp, 0x4e, 0x84)
        self.pi.i2c_write_byte_data(self.comp, 0x51, 0x04)
        self.pi.i2c_write_byte_data(self.comp, 0x52, 0x16)

        sleep(0.3)

    def read_accel(self):
        x_l = self.pi.i2c_read_byte_data(self.accel, 0x02)
        x_m = self.pi.i2c_read_byte_data(self.accel, 0x03)
        y_l = self.pi.i2c_read_byte_data(self.accel, 0x04)
        y_m = self.pi.i2c_read_byte_data(self.accel, 0x05)
        z_l = self.pi.i2c_read_byte_data(self.accel, 0x06)
        z_m = self.pi.i2c_read_byte_data(self.accel, 0x07)

        x = ((x_m*256) + (x_l&0xf0))/16
        if (x > 2047):
            x -= 4096
        y = ((y_m*256) + (y_l&0xf0))/16
        if (y > 2047):
            y -= 4096
        z = ((x_m*256) + (z_l&0xf0))/16
        if (z > 2047):
            z -= 4096

        return (x, y, z)


    def read_compass(self):
        x_l = self.pi.i2c_read_byte_data(self.comp, 0x42)
        x_m = self.pi.i2c_read_byte_data(self.comp, 0x43)
        y_l = self.pi.i2c_read_byte_data(self.comp, 0x44)
        y_m = self.pi.i2c_read_byte_data(self.comp, 0x45)
        z_l = self.pi.i2c_read_byte_data(self.comp, 0x46)
        z_m = self.pi.i2c_read_byte_data(self.comp, 0x47)
        t_l = self.pi.i2c_read_byte_data(self.comp, 0x48)
        t_m = self.pi.i2c_read_byte_data(self.comp, 0x49)

        x = (x_m << 8) + (x_l >> 3)
        if (x > 4095):
            x -= 8192
        y = (y_m << 8) + (y_l >> 3)
        if (y > 4095):
            y -= 8192
        z = (x_m << 8) + (z_l >> 3)
        if (z > 16383):
            z -= 32768

        return (x, y, z)

    def read_gyro(self):
        x_l = self.pi.i2c_read_byte_data(self.gyro, 0x02)
        x_m = self.pi.i2c_read_byte_data(self.gyro, 0x03)
        y_l = self.pi.i2c_read_byte_data(self.gyro, 0x04)
        y_m = self.pi.i2c_read_byte_data(self.gyro, 0x05)
        z_l = self.pi.i2c_read_byte_data(self.gyro, 0x06)
        z_m = self.pi.i2c_read_byte_data(self.gyro, 0x07)

        x = (x_m*256) + x_l
        if (x > 32767):
            x -= 65536
        y = (y_m*256) + y_l
        if (y > 32767):
            y -= 65536
        z = (x_m*256) + z_l
        if (z > 32767):
            z -= 65536

        return (x, y, z)

Servo motor (SG92) controlled by PWM

Servo motor (SG92) specification table

item value Remarks
Movable angle 180 degrees
speed 0.1s /60 degrees
Voltage 4.8 ~ 5.0V
Pulse period 10,000 - 20,000μs
pulse width 500 - 2,400μs ※Data sheet:1,000 - 2,000μs

SG90.py


import pigpio

class SG90:
    """SG90_Spec:
    #Movable angle:180 degrees
    #speed: 0.1s /60 degrees
    #Voltage: 4.8 ~ 5V
    #Pulse period: 10,000 - 20,000μs
    #pulse width: 500 - 2,400μs * Data sheet:1,000 - 2,000μs
    """
    def __init__(self, pin=None):
        self.range_of_motion = 180
        self.min_pulse_width = 500
        self.max_pulse_width = 2400
        self.pig = pigpio.pi()
        self.pin = pin
    
    def move(self, rad):
        if self.pin == None:
            pass
        else:
            spw = (rad/self.range_of_motion) * (self.max_pulse_width-self.min_pulse_width) + self.min_pulse_width
            self.pig.set_servo_pulsewidth(self.pin, spw)

    def stop(self):
        self.pig.set_servo_pulsewidth(self.pin, 0)

in conclusion

--Investigate how to use motion sensors, infrared sensors, temperature / humidity sensors, and illuminance sensors. --I want RaspberryPi 4.

Recommended Posts

pigpio on Raspberry pi
Cython on Raspberry Pi
Introduced pyenv on Raspberry Pi
Use NeoPixel on Raspberry Pi
Install OpenCV4 on Raspberry Pi 3
Install TensorFlow 1.15.0 on Raspberry Pi
MQTT on Raspberry Pi and Mac
raspberry pi 4 centos7 install on docker
Try using ArUco on Raspberry Pi
OpenCV installation procedure on Raspberry Pi
Power on / off Raspberry pi on Arduino
Detect switch status on Raspberry Pi 3
Install OpenMedia Vault 5 on Raspberry Pi 4
L Chika on Raspberry Pi C #
Build wxPython on Ubuntu 20.04 on raspberry pi 4
Raspberry Pi backup
"Honwaka Notification Lamp" on Raspberry Pi Part 2
Detect "brightness" using python on Raspberry Pi 3!
USB boot on Raspberry Pi 4 Model B
"Honwaka Notification Lamp" on Raspberry Pi Part 1
Enable UART + serial communication on Raspberry Pi
Adafruit Python BluefruitLE works on Raspberry Pi.
Accelerate Deep Learning on Raspberry Pi 4 CPU
Set swap space on Ubuntu on Raspberry Pi
Programming normally with Node-RED programming on Raspberry Pi 3
Use the Grove sensor on the Raspberry Pi
Install 64-bit OS (bate) on Raspberry Pi
Install docker-compose on 64-bit Raspberry Pi OS
Run servomotor on Raspberry Pi 3 using python
"Honwaka Notification Lamp" on Raspberry Pi Part 3
Working with sensors on Mathematica on Raspberry Pi
Build OpenCV-Python environment on Raspberry Pi B +
Detect temperature using python on Raspberry Pi 3!
Mount Windows shared folder on Raspberry Pi
Matrix multiplication on Raspberry Pi GPU (Part 2)
How to install NumPy on Raspberry Pi
I installed OpenCV-Python on my Raspberry Pi
Working with GPS on Raspberry Pi 3 Python
What is Raspberry Pi?
Why detectMultiScale () is slow on Raspberry Pi B +
GPGPU with Raspberry Pi
Detect slide switches using python on Raspberry Pi 3!
Build a Django environment on Raspberry Pi (MySQL)
Try using a QR code on a Raspberry Pi
Detect magnet switches using python on Raspberry Pi 3!
Raspberry Pi video camera
Enjoy electronic work with GPIO on Raspberry Pi
Raspberry Pi Bad Knowledge
Let's do Raspberry Pi?
Power on / off your PC with raspberry pi
Use Grove-Temperature & Humidity Sensor (DHT11) on Raspberry Pi
Make DHT11 available on Raspberry Pi + python (memo)
Beginning cross-compilation for Raspberry Pi Zero on Ubuntu
Sound the buzzer using python on Raspberry Pi 3!
Play with your Ubuntu desktop on your Raspberry Pi 4
Display CPU temperature every 5 seconds on Raspberry Pi 4
DigitalSignage with Raspberry Pi
Introduced Ceph on Kubernetes on Raspberry Pi 4B (ARM64)
Raspberry Pi 4 setup memo
Connect to MySQL with Python on Raspberry Pi
Raspberry Pi system monitoring