pigpio is a Python module for the Raspberry which talks to the pigpio daemon to allow control of the general purpose input outputs (GPIO).
pigpio.exceptions = False
# Code where you want to test the error status.
pigpio.exceptions = True
pi1 = pigpio.pi() # pi1 accesses the local Pi's GPIO
pi2 = pigpio.pi('tom') # pi2 accesses tom's GPIO
pi3 = pigpio.pi('dick') # pi3 accesses dick's GPIO
pi1.write(4, 0) # set local Pi's GPIO 4 low
pi2.write(4, 1) # set tom's GPIO 4 to high
pi3.read(4) # get level of dick's GPIO 4
ESSENTIAL | |
pigpio.pi | Initialise Pi connection |
stop | Stop a Pi connection |
BASIC | |
set_mode | Set a GPIO mode |
get_mode | Get a GPIO mode |
set_pull_up_down | Set/clear GPIO pull up/down resistor |
read | Read a GPIO |
write | Write a GPIO |
PWM (overrides servo commands on same GPIO) | |
set_PWM_dutycycle | Start/stop PWM pulses on a GPIO |
set_PWM_frequency | Set PWM frequency of a GPIO |
set_PWM_range | Configure PWM range of a GPIO |
get_PWM_dutycycle | Get PWM dutycycle set on a GPIO |
get_PWM_frequency | Get PWM frequency of a GPIO |
get_PWM_range | Get configured PWM range of a GPIO |
get_PWM_real_range | Get underlying PWM range for a GPIO |
Servo (overrides PWM commands on same GPIO) | |
set_servo_pulsewidth | Start/Stop servo pulses on a GPIO |
get_servo_pulsewidth | Get servo pulsewidth set on a GPIO |
INTERMEDIATE | |
gpio_trigger | Send a trigger pulse to a GPIO |
set_watchdog | Set a watchdog on a GPIO |
read_bank_1 | Read all bank 1 GPIO |
read_bank_2 | Read all bank 2 GPIO |
clear_bank_1 | Clear selected GPIO in bank 1 |
clear_bank_2 | Clear selected GPIO in bank 2 |
set_bank_1 | Set selected GPIO in bank 1 |
set_bank_2 | Set selected GPIO in bank 2 |
callback | Create GPIO level change callback |
wait_for_edge | Wait for GPIO level change |
ADVANCED | |
notify_open | Request a notification handle |
notify_begin | Start notifications for selected GPIO |
notify_pause | Pause notifications |
notify_close | Close a notification |
hardware_clock | Start hardware clock on supported GPIO |
hardware_PWM | Start hardware PWM on supported GPIO |
set_glitch_filter | Set a glitch filter on a GPIO |
set_noise_filter | Set a noise filter on a GPIO |
set_pad_strength | Sets a pads drive strength |
get_pad_strength | Gets a pads drive strength |
shell | Executes a shell command |
Custom | |
custom_1 | User custom function 1 |
custom_2 | User custom function 2 |
Events | |
event_callback | Sets a callback for an event |
event_trigger | Triggers an event |
wait_for_event | Wait for an event |
Scripts | |
store_script | Store a script |
run_script | Run a stored script |
update_script | Set a scripts parameters |
script_status | Get script status and parameters |
stop_script | Stop a running script |
delete_script | Delete a stored script |
I2C | |
i2c_open | Opens an I2C device |
i2c_close | Closes an I2C device |
i2c_write_quick | SMBus write quick |
i2c_read_byte | SMBus read byte |
i2c_write_byte | SMBus write byte |
i2c_read_byte_data | SMBus read byte data |
i2c_write_byte_data | SMBus write byte data |
i2c_read_word_data | SMBus read word data |
i2c_write_word_data | SMBus write word data |
i2c_read_block_data | SMBus read block data |
i2c_write_block_data | SMBus write block data |
i2c_read_i2c_block_data | SMBus read I2C block data |
i2c_write_i2c_block_data | SMBus write I2C block data |
i2c_read_device | Reads the raw I2C device |
i2c_write_device | Writes the raw I2C device |
i2c_process_call | SMBus process call |
i2c_block_process_call | SMBus block process call |
i2c_zip | Performs multiple I2C transactions |
I2C BIT BANG | |
bb_i2c_open | Opens GPIO for bit banging I2C |
bb_i2c_close | Closes GPIO for bit banging I2C |
bb_i2c_zip | Performs multiple bit banged I2C transactions |
I2C/SPI SLAVE | |
bsc_xfer | I2C/SPI as slave transfer |
bsc_i2c | I2C as slave transfer |
SERIAL | |
serial_open | Opens a serial device |
serial_close | Closes a serial device |
serial_read_byte | Reads a byte from a serial device |
serial_write_byte | Writes a byte to a serial device |
serial_read | Reads bytes from a serial device |
serial_write | Writes bytes to a serial device |
serial_data_available | Returns number of bytes ready to be read |
SERIAL BIT BANG (read only) | |
bb_serial_read_open | Open a GPIO for bit bang serial reads |
bb_serial_read_close | Close a GPIO for bit bang serial reads |
bb_serial_invert | Invert serial logic (1 invert, 0 normal) |
bb_serial_read | Read bit bang serial data from a GPIO |
SPI | |
spi_open | Opens a SPI device |
spi_close | Closes a SPI device |
spi_read | Reads bytes from a SPI device |
spi_write | Writes bytes to a SPI device |
spi_xfer | Transfers bytes with a SPI device |
SPI BIT BANG | |
bb_spi_open | Opens GPIO for bit banging SPI |
bb_spi_close | Closes GPIO for bit banging SPI |
bb_spi_xfer | Transfers bytes with bit banging SPI |
FILES | |
file_open | Opens a file |
file_close | Closes a file |
file_read | Reads bytes from a file |
file_write | Writes bytes to a file |
file_seek | Seeks to a position within a file |
file_list | List files which match a pattern |
WAVES | |
wave_clear | Deletes all waveforms |
wave_add_new | Starts a new waveform |
wave_add_generic | Adds a series of pulses to the waveform |
wave_add_serial | Adds serial data to the waveform |
wave_create | Creates a waveform from added data |
wave_create_and_pad | Creates a waveform of fixed size from added data |
wave_delete | Deletes a waveform |
wave_send_once | Transmits a waveform once |
wave_send_repeat | Transmits a waveform repeatedly |
wave_send_using_mode | Transmits a waveform in the chosen mode |
wave_chain | Transmits a chain of waveforms |
wave_tx_at | Returns the current transmitting waveform |
wave_tx_busy | Checks to see if a waveform has ended |
wave_tx_stop | Aborts the current waveform |
wave_get_cbs | Length in cbs of the current waveform |
wave_get_max_cbs | Absolute maximum allowed cbs |
wave_get_micros | Length in microseconds of the current waveform |
wave_get_max_micros | Absolute maximum allowed micros |
wave_get_pulses | Length in pulses of the current waveform |
wave_get_max_pulses | Absolute maximum allowed pulses |
UTILITIES | |
get_current_tick | Get current tick (microseconds) |
get_hardware_revision | Get hardware revision |
get_pigpio_version | Get the pigpio version |
pigpio.error_text | Gets error text from error number |
pigpio.tickDiff | Returns difference between two ticks |
pi = pigio.pi() # use defaults
pi = pigpio.pi('mypi') # specify host, default port
pi = pigpio.pi('mypi', 7777) # specify host and port
pi = pigpio.pi() # exit script if no connection
if not pi.connected:
exit()
pi.bb_i2c_close(SDA)
h = pi.bb_i2c_open(4, 5, 50000) # bit bang on GPIO 4/5 at 50kbps
(count, data) = pi.bb_i2c_zip(
SDA, [4, 0x53, 2, 7, 1, 0x32, 2, 6, 6, 3, 0])
Name | Cmd & Data | Meaning |
End | 0 | No more commands |
Escape | 1 | Next P is two bytes |
Start | 2 | Start condition |
Stop | 3 | Stop condition |
Address | 4 P | Set I2C address to P |
Flags | 5 lsb msb | Set I2C flags to lsb + (msb << 8) |
Read | 6 P | Read P bytes of data |
Write | 7 P ... | Write P bytes of data |
Set address 0x53
start, write 0x32, (re)start, read 6 bytes, stop
Set address 0x1E
start, write 0x03, (re)start, read 6 bytes, stop
Set address 0x68
start, write 0x1B, (re)start, read 8 bytes, stop
End
0x04 0x53
0x02 0x07 0x01 0x32 0x02 0x06 0x06 0x03
0x04 0x1E
0x02 0x07 0x01 0x03 0x02 0x06 0x06 0x03
0x04 0x68
0x02 0x07 0x01 0x1B 0x02 0x06 0x08 0x03
0x00
status = pi.bb_serial_invert(17, 1)
(count, data) = pi.bb_serial_read(4)
status = pi.bb_serial_read_close(17)
status = pi.bb_serial_read_open(4, 19200)
status = pi.bb_serial_read_open(17, 9600)
pi.bb_spi_close(CS)
21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0 0 0 0 0 0 R T 0 0 0 0 0 0 0 0 0 0 0 p m m
Mode CPOL CPHA
0 0 0
1 0 1
2 1 0
3 1 1
pigpio.SPI_MODE_0
pigpio.SPI_MODE_1
pigpio.SPI_MODE_2
pigpio.SPI_MODE_3
bb_spi_open(10, MISO, MOSI, SCLK, 10000, 0); // device 1
bb_spi_open(11, MISO, MOSI, SCLK, 20000, 3); // device 2
#!/usr/bin/env python
import pigpio
CE0=5
CE1=6
MISO=13
MOSI=19
SCLK=12
pi = pigpio.pi()
if not pi.connected:
exit()
pi.bb_spi_open(CE0, MISO, MOSI, SCLK, 10000, 0) # MCP4251 DAC
pi.bb_spi_open(CE1, MISO, MOSI, SCLK, 20000, 3) # MCP3008 ADC
for i in range(256):
count, data = pi.bb_spi_xfer(CE0, [0, i]) # Set DAC value
if count == 2:
count, data = pi.bb_spi_xfer(CE0, [12, 0]) # Read back DAC
if count == 2:
set_val = data[1]
count, data = pi.bb_spi_xfer(CE1, [1, 128, 0]) # Read ADC
if count == 3:
read_val = ((data[1]&3)<<8) | data[2]
print("{} {}".format(set_val, read_val))
pi.bb_spi_close(CE0)
pi.bb_spi_close(CE1)
pi.stop()
#!/usr/bin/env python
import time
import pigpio
I2C_ADDR=0x13
def i2c(id, tick):
global pi
s, b, d = pi.bsc_i2c(I2C_ADDR)
if b:
if d[0] == ord('t'): # 116 send 'HH:MM:SS*'
print("sent={} FR={} received={} [{}]".
format(s>>16, s&0xfff,b,d))
s, b, d = pi.bsc_i2c(I2C_ADDR,
"{}*".format(time.asctime()[11:19]))
elif d[0] == ord('d'): # 100 send 'Sun Oct 30*'
print("sent={} FR={} received={} [{}]".
format(s>>16, s&0xfff,b,d))
s, b, d = pi.bsc_i2c(I2C_ADDR,
"{}*".format(time.asctime()[:10]))
pi = pigpio.pi()
if not pi.connected:
exit()
# Respond to BSC slave activity
e = pi.event_callback(pigpio.EVENT_BSC, i2c)
pi.bsc_i2c(I2C_ADDR) # Configure BSC as I2C slave
time.sleep(600)
e.cancel()
pi.bsc_i2c(0) # Disable BSC peripheral
pi.stop()
$ i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- 13 -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
$ pigs i2co 1 0x13 0
0
$ pigs i2cwd 0 116
$ pigs i2crd 0 9 -a
9 10:13:58*
$ pigs i2cwd 0 116
$ pigs i2crd 0 9 -a
9 10:14:29*
$ pigs i2cwd 0 100
$ pigs i2crd 0 11 -a
11 Sun Oct 30*
$ pigs i2cwd 0 100
$ pigs i2crd 0 11 -a
11 Sun Oct 30*
$ pigs i2cwd 0 116
$ pigs i2crd 0 9 -a
9 10:23:16*
$ pigs i2cwd 0 100
$ pigs i2crd 0 11 -a
11 Sun Oct 30*
SDA | SCL | MOSI | SCLK | MISO | CE | |
I2C | 18 | 19 | - | - | - | - |
SPI | - | - | 18 | 19 | 20 | 21 |
SDA | SCL | MOSI | SCLK | MISO | CE | |
I2C | 10 | 11 | - | - | - | - |
SPI | - | - | 10 | 11 | 9 | 8 |
22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
a a a a a a a - - IT HC TF IR RE TE BK EC ES PL PH I2 SP EN
aaaaaaa | defines the I2C slave address (only relevant in I2C mode) |
IT | invert transmit status flags |
HC | enable host control |
TF | enable test FIFO |
IR | invert receive status flags |
RE | enable receive |
TE | enable transmit |
BK | abort operation and clear FIFOs |
EC | send control register as first I2C byte |
ES | send status register as first I2C byte |
PL | set SPI polarity high |
PH | set SPI phase high |
I2 | enable I2C mode |
SP | enable SPI mode |
EN | enable BSC peripheral |
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
S S S S S R R R R R T T T T T RB TE RF TF RE TB
SSSSS | number of bytes successfully copied to transmit FIFO |
RRRRR | number of bytes in receieve FIFO |
TTTTT | number of bytes in transmit FIFO |
RB | receive busy |
TE | transmit FIFO empty |
RF | receive FIFO full |
TF | transmit FIFO full |
RE | receive FIFO empty |
TB | transmit busy |
(status, count, data) = pi.bsc_xfer(0x330305, "Hello!")
Parameter Value Meaning
GPIO 0-31 The GPIO which has changed state
level 0-2 0 = change to low (a falling edge)
1 = change to high (a rising edge)
2 = no level change (a watchdog timeout)
tick 32 bit The number of microseconds since boot
WARNING: this wraps around from
4294967295 to 0 roughly every 72 minutes
samples
per sec
1 1,000,000
2 500,000
sample 4 250,000
rate 5 200,000
(us) 8 125,000
10 100,000
def cbf(gpio, level, tick):
print(gpio, level, tick)
cb1 = pi.callback(22, pigpio.EITHER_EDGE, cbf)
cb2 = pi.callback(4, pigpio.EITHER_EDGE)
cb3 = pi.callback(17)
print(cb3.tally())
cb3.reset_tally()
cb1.cancel() # To cancel callback cb1.
pi.clear_bank_1(int("111110010000",2))
pi.clear_bank_2(0x1010)
value = pi.custom_1()
value = pi.custom_1(23)
value = pi.custom_1(0, 55)
value = pi.custom_1(23, 56, [1, 5, 7])
value = pi.custom_1(23, 56, b"hello")
value = pi.custom_1(23, 56, "hello")
(count, data) = pi.custom_2()
(count, data) = pi.custom_2(23)
(count, data) = pi.custom_2(23, [1, 5, 7])
(count, data) = pi.custom_2(23, b"hello")
(count, data) = pi.custom_2(23, "hello", 128)
status = pi.delete_script(sid)
def cbf(event, tick):
print(event, tick)
cb1 = pi.event_callback(22, cbf)
cb2 = pi.event_callback(4)
print(cb2.tally())
cb2.reset_tally()
cb1.event_cancel() # To cancel callback cb1.
pi.event_trigger(23)
pi.file_close(handle)
#!/usr/bin/env python
import pigpio
pi = pigpio.pi()
if not pi.connected:
exit()
# Assumes /opt/pigpio/access contains the following line:
# /ram/*.c r
c, d = pi.file_list("/ram/p*.c")
if c > 0:
print(d)
pi.stop()
h = pi.file_open("/home/pi/shared/dir_3/file.txt",
pigpio.FILE_WRITE | pigpio.FILE_CREATE)
pi.file_write(h, "Hello world")
pi.file_close(h)
/home/* n
/home/pi/shared/dir_1/* w
/home/pi/shared/dir_2/* r
/home/pi/shared/dir_3/* u
/home/pi/shared/dir_1/file.txt n
a path containing ..
a path containing only wildcards (*?)
a path containing less than two non-wildcard parts
Constant | Value | Meaning |
FILE_READ | 1 | open file for reading |
FILE_WRITE | 2 | open file for writing |
FILE_RW | 3 | open file for reading and writing |
Name | Value | Meaning |
FILE_APPEND | 4 | All writes append data to the end of the file |
FILE_CREATE | 8 | The file is created if it doesn't exist |
FILE_TRUNC | 16 | The file is truncated |
#!/usr/bin/env python
import pigpio
pi = pigpio.pi()
if not pi.connected:
exit()
# Assumes /opt/pigpio/access contains the following line:
# /ram/*.c r
handle = pi.file_open("/ram/pigpio.c", pigpio.FILE_READ)
done = False
while not done:
c, d = pi.file_read(handle, 60000)
if c > 0:
print(d)
else:
done = True
pi.file_close(handle)
pi.stop()
(b, d) = pi.file_read(h2, 100)
if b > 0:
# process read data
new_pos = pi.file_seek(h, 100, pigpio.FROM_START)
cur_pos = pi.file_seek(h, 0, pigpio.FROM_CURRENT)
file_size = pi.file_seek(h, 0, pigpio.FROM_END)
pi.file_write(h1, b'\x02\x03\x04')
pi.file_write(h2, b'help')
pi.file_write(h2, "hello")
pi.file_write(h1, [2, 3, 4])
pi.set_PWM_dutycycle(4, 25)
print(pi.get_PWM_dutycycle(4))
25
pi.set_PWM_dutycycle(4, 203)
print(pi.get_PWM_dutycycle(4))
203
pi.set_PWM_frequency(4,0)
print(pi.get_PWM_frequency(4))
10
pi.set_PWM_frequency(4, 800)
print(pi.get_PWM_frequency(4))
800
pi.set_PWM_range(9, 500)
print(pi.get_PWM_range(9))
500
pi.set_PWM_frequency(4, 800)
print(pi.get_PWM_real_range(4))
250
t1 = pi.get_current_tick()
time.sleep(1)
t2 = pi.get_current_tick()
print(pi.get_hardware_revision())
2
0 = INPUT
1 = OUTPUT
2 = ALT5
3 = ALT4
4 = ALT0
5 = ALT1
6 = ALT2
7 = ALT3
print(pi.get_mode(0))
4
Pad | GPIO |
0 | 0-27 |
1 | 28-45 |
2 | 46-53 |
strength = pi.get_pad_strength(0) # Get pad 0 strength.
v = pi.get_pigpio_version()
pi.set_servo_pulsewidth(4, 525)
print(pi.get_servo_pulsewidth(4))
525
pi.set_servo_pulsewidth(4, 2130)
print(pi.get_servo_pulsewidth(4))
2130
pi.gpio_trigger(23, 10, 1)
12 PWM channel 0 All models but A and B
13 PWM channel 1 All models but A and B
18 PWM channel 0 All models
19 PWM channel 1 All models but A and B
40 PWM channel 0 Compute module only
41 PWM channel 1 Compute module only
45 PWM channel 1 Compute module only
52 PWM channel 0 Compute module only
53 PWM channel 1 Compute module only
pi.hardware_PWM(18, 800, 250000) # 800Hz 25% dutycycle
pi.hardware_PWM(18, 2000, 750000) # 2000Hz 75% dutycycle
4 clock 0 All models
5 clock 1 All models but A and B (reserved for system use)
6 clock 2 All models but A and B
20 clock 0 All models but A and B
21 clock 1 All models but A and Rev.2 B (reserved for system use)
32 clock 0 Compute module only
34 clock 0 Compute module only
42 clock 1 Compute module only (reserved for system use)
43 clock 2 Compute module only
44 clock 1 Compute module only (reserved for system use)
pi.hardware_clock(4, 5000) # 5 KHz clock on GPIO 4
pi.hardware_clock(4, 40000000) # 40 MHz clock on GPIO 4
S Addr Wr [A] reg [A] len(data) [A] data0 [A] ... datan [A]
S Addr Rd [A] [Count] A [Data] ... A P
(b, d) = pi.i2c_block_process_call(h, 10, b'\x02\x05\x00')
(b, d) = pi.i2c_block_process_call(h, 10, b'abcdr')
(b, d) = pi.i2c_block_process_call(h, 10, "abracad")
(b, d) = pi.i2c_block_process_call(h, 10, [2, 5, 16])
pi.i2c_close(h)
SDA | SCL | |
I2C 0 | 0 | 1 |
I2C 1 | 2 | 3 |
S (1 bit) : Start bit
P (1 bit) : Stop bit
Rd/Wr (1 bit) : Read/Write bit. Rd equals 1, Wr equals 0.
A, NA (1 bit) : Accept and not accept bit.
Addr (7 bits): I2C 7 bit address.
reg (8 bits): Command byte, which often selects a register.
Data (8 bits): A data byte.
Count (8 bits): A byte defining the length of a block operation.
[..]: Data sent by the device.
h = pi.i2c_open(1, 0x53) # open device at address 0x53 on bus 1
S Addr Wr [A] reg [A] word_val_Low [A] word_val_High [A]
S Addr Rd [A] [DataLow] A [DataHigh] NA P
r = pi.i2c_process_call(h, 4, 0x1231)
r = pi.i2c_process_call(h, 6, 0)
S Addr Wr [A] reg [A]
S Addr Rd [A] [Count] A [Data] A [Data] A ... A [Data] NA P
(b, d) = pi.i2c_read_block_data(h, 10)
if b >= 0:
# process data
else:
# process read failure
S Addr Rd [A] [Data] NA P
b = pi.i2c_read_byte(2) # read a byte from device 2
S Addr Wr [A] reg [A] S Addr Rd [A] [Data] NA P
# read byte from reg 17 of device 2
b = pi.i2c_read_byte_data(2, 17)
# read byte from reg 1 of device 0
b = pi.i2c_read_byte_data(0, 1)
S Addr Rd [A] [Data] A [Data] A ... A [Data] NA P
(count, data) = pi.i2c_read_device(h, 12)
S Addr Wr [A] reg [A]
S Addr Rd [A] [Data] A [Data] A ... A [Data] NA P
(b, d) = pi.i2c_read_i2c_block_data(h, 4, 32)
if b >= 0:
# process data
else:
# process read failure
S Addr Wr [A] reg [A] S Addr Rd [A] [DataLow] A [DataHigh] NA P
# read word from reg 2 of device 3
w = pi.i2c_read_word_data(3, 2)
# read word from reg 7 of device 2
w = pi.i2c_read_word_data(2, 7)
S Addr Wr [A] reg [A] len(data) [A] data0 [A] data1 [A] ... [A]
datan [A] P
pi.i2c_write_block_data(4, 5, b'hello')
pi.i2c_write_block_data(4, 5, "data bytes")
pi.i2c_write_block_data(5, 0, b'\x00\x01\x22')
pi.i2c_write_block_data(6, 2, [0, 1, 0x22])
S Addr Wr [A] byte_val [A] P
pi.i2c_write_byte(1, 17) # send byte 17 to device 1
pi.i2c_write_byte(2, 0x23) # send byte 0x23 to device 2
S Addr Wr [A] reg [A] byte_val [A] P
# send byte 0xC5 to reg 2 of device 1
pi.i2c_write_byte_data(1, 2, 0xC5)
# send byte 9 to reg 4 of device 2
pi.i2c_write_byte_data(2, 4, 9)
S Addr Wr [A] data0 [A] data1 [A] ... [A] datan [A] P
pi.i2c_write_device(h, b"\x12\x34\xA8")
pi.i2c_write_device(h, b"help")
pi.i2c_write_device(h, 'help')
pi.i2c_write_device(h, [23, 56, 231])
S Addr Wr [A] reg [A] data0 [A] data1 [A] ... [A] datan [NA] P
pi.i2c_write_i2c_block_data(4, 5, 'hello')
pi.i2c_write_i2c_block_data(4, 5, b'hello')
pi.i2c_write_i2c_block_data(5, 0, b'\x00\x01\x22')
pi.i2c_write_i2c_block_data(6, 2, [0, 1, 0x22])
S Addr bit [A] P
pi.i2c_write_quick(0, 1) # send 1 to device 0
pi.i2c_write_quick(3, 0) # send 0 to device 3
S Addr Wr [A] reg [A] word_val_Low [A] word_val_High [A] P
# send word 0xA0C5 to reg 5 of device 4
pi.i2c_write_word_data(4, 5, 0xA0C5)
# send word 2 to reg 2 of device 5
pi.i2c_write_word_data(5, 2, 23)
(count, data) = pi.i2c_zip(h, [4, 0x53, 7, 1, 0x32, 6, 6, 0])
Name | Cmd & Data | Meaning |
End | 0 | No more commands |
Escape | 1 | Next P is two bytes |
On | 2 | Switch combined flag on |
Off | 3 | Switch combined flag off |
Address | 4 P | Set I2C address to P |
Flags | 5 lsb msb | Set I2C flags to lsb + (msb << 8) |
Read | 6 P | Read P bytes of data |
Write | 7 P ... | Write P bytes of data |
Set address 0x53, write 0x32, read 6 bytes
Set address 0x1E, write 0x03, read 6 bytes
Set address 0x68, write 0x1B, read 8 bytes
End
0x04 0x53 0x07 0x01 0x32 0x06 0x06
0x04 0x1E 0x07 0x01 0x03 0x06 0x06
0x04 0x68 0x07 0x01 0x1B 0x06 0x08
0x00
h = pi.notify_open()
if h >= 0:
pi.notify_begin(h, 1234)
h = pi.notify_open()
if h >= 0:
pi.notify_begin(h, 1234)
...
pi.notify_close(h)
...
H seqno
H flags
I tick
I level
h = pi.notify_open()
if h >= 0:
pi.notify_begin(h, 1234)
h = pi.notify_open()
if h >= 0:
pi.notify_begin(h, 1234)
...
pi.notify_pause(h)
...
pi.notify_begin(h, 1234)
...
pi.set_mode(23, pigpio.INPUT)
pi.set_pull_up_down(23, pigpio.PUD_DOWN)
print(pi.read(23))
0
pi.set_pull_up_down(23, pigpio.PUD_UP)
print(pi.read(23))
1
print(bin(pi.read_bank_1()))
0b10010100000011100100001001111
print(bin(pi.read_bank_2()))
0b1111110000000000000000
s = pi.run_script(sid, [par1, par2])
s = pi.run_script(sid)
s = pi.run_script(sid, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
PI_SCRIPT_INITING
PI_SCRIPT_HALTED
PI_SCRIPT_RUNNING
PI_SCRIPT_WAITING
PI_SCRIPT_FAILED
(s, pars) = pi.script_status(sid)
pi.serial_close(h1)
rdy = pi.serial_data_available(h1)
if rdy > 0:
(b, d) = pi.serial_read(h1, rdy)
h1 = pi.serial_open("/dev/ttyAMA0", 300)
h2 = pi.serial_open("/dev/ttyUSB1", 19200, 0)
h3 = pi.serial_open("/dev/serial0", 9600)
(b, d) = pi.serial_read(h2, 100)
if b > 0:
# process read data
b = pi.serial_read_byte(h1)
pi.serial_write(h1, b'\x02\x03\x04')
pi.serial_write(h2, b'help')
pi.serial_write(h2, "hello")
pi.serial_write(h1, [2, 3, 4])
pi.serial_write_byte(h1, 23)
pi.serial_write_byte(h1, ord('Z'))
pi.set_PWM_dutycycle(4, 0) # PWM off
pi.set_PWM_dutycycle(4, 64) # PWM 1/4 on
pi.set_PWM_dutycycle(4, 128) # PWM 1/2 on
pi.set_PWM_dutycycle(4, 192) # PWM 3/4 on
pi.set_PWM_dutycycle(4, 255) # PWM full on
Hertz
1: 40000 20000 10000 8000 5000 4000 2500 2000 1600
1250 1000 800 500 400 250 200 100 50
2: 20000 10000 5000 4000 2500 2000 1250 1000 800
625 500 400 250 200 125 100 50 25
4: 10000 5000 2500 2000 1250 1000 625 500 400
313 250 200 125 100 63 50 25 13
sample
rate
(us) 5: 8000 4000 2000 1600 1000 800 500 400 320
250 200 160 100 80 50 40 20 10
8: 5000 2500 1250 1000 625 500 313 250 200
156 125 100 63 50 31 25 13 6
10: 4000 2000 1000 800 500 400 250 200 160
125 100 80 50 40 25 20 10 5
pi.set_PWM_frequency(4,0)
print(pi.get_PWM_frequency(4))
10
pi.set_PWM_frequency(4,100000)
print(pi.get_PWM_frequency(4))
8000
pi.set_PWM_range(9, 100) # now 25 1/4, 50 1/2, 75 3/4 on
pi.set_PWM_range(9, 500) # now 125 1/4, 250 1/2, 375 3/4 on
pi.set_PWM_range(9, 3000) # now 750 1/4, 1500 1/2, 2250 3/4 on
pi.set_bank_1(int("111110010000",2))
pi.set_bank_2(0x303)
pi.set_glitch_filter(23, 100)
pi.set_mode( 4, pigpio.INPUT) # GPIO 4 as input
pi.set_mode(17, pigpio.OUTPUT) # GPIO 17 as output
pi.set_mode(24, pigpio.ALT2) # GPIO 24 as ALT2
pi.set_noise_filter(23, 1000, 5000)
Pad | GPIO |
0 | 0-27 |
1 | 28-45 |
2 | 46-53 |
pi.set_pad_strength(2, 14) # Set pad 2 to 14 mA.
pi.set_pull_up_down(17, pigpio.PUD_OFF)
pi.set_pull_up_down(23, pigpio.PUD_UP)
pi.set_pull_up_down(24, pigpio.PUD_DOWN)
pi.set_servo_pulsewidth(17, 0) # off
pi.set_servo_pulsewidth(17, 1000) # safe anti-clockwise
pi.set_servo_pulsewidth(17, 1500) # centre
pi.set_servo_pulsewidth(17, 2000) # safe clockwise
pi.set_watchdog(23, 1000) # 1000 ms watchdog on GPIO 23
pi.set_watchdog(23, 0) # cancel watchdog on GPIO 23
Script exit status | Returned system call status |
1 | 256 |
5 | 1280 |
10 | 2560 |
200 | 51200 |
script not found | 32512 |
// pass two parameters, hello and world
status = pi.shell("scr1", "hello world");
// pass three parameters, hello, string with spaces, and world
status = pi.shell("scr1", "hello 'string with spaces' world");
// pass one parameter, hello string with spaces world
status = pi.shell("scr1", "\"hello string with spaces world\"");
pi.spi_close(h)
MISO | MOSI | SCLK | CE0 | CE1 | CE2 | |
Main SPI | 9 | 10 | 11 | 8 | 7 | - |
Aux SPI | 19 | 20 | 21 | 18 | 17 | 16 |
21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
b b b b b b R T n n n n W A u2 u1 u0 p2 p1 p0 m m
Mode POL PHA
0 0 0
1 0 1
2 1 0
3 1 1
# open SPI device on channel 1 in mode 3 at 50000 bits per second
h = pi.spi_open(1, 50000, 3)
(b, d) = pi.spi_read(h, 60) # read 60 bytes from device h
if b == 60:
# process read data
else:
# error path
pi.spi_write(0, b'\x02\xc0\x80') # write 3 bytes to device 0
pi.spi_write(0, b'defgh') # write 5 bytes to device 0
pi.spi_write(0, "def") # write 3 bytes to device 0
pi.spi_write(1, [2, 192, 128]) # write 3 bytes to device 1
(count, rx_data) = pi.spi_xfer(h, b'\x01\x80\x00')
(count, rx_data) = pi.spi_xfer(h, [1, 128, 0])
(count, rx_data) = pi.spi_xfer(h, b"hello")
(count, rx_data) = pi.spi_xfer(h, "hello")
pi.stop()
status = pi.stop_script(sid)
sid = pi.store_script(
b'tag 0 w 22 1 mils 100 w 22 0 mils 100 dcr p0 jp 0')
s = pi.update_script(sid, [par1, par2])
s = pi.update_script(sid, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
if pi.wait_for_edge(23):
print("Rising edge detected")
else:
print("wait for edge timed out")
if pi.wait_for_edge(23, pigpio.FALLING_EDGE, 5.0):
print("Falling edge detected")
else:
print("wait for falling edge timed out")
if pi.wait_for_event(23):
print("event detected")
else:
print("wait for event timed out")
G1=4
G2=24
pi.set_mode(G1, pigpio.OUTPUT)
pi.set_mode(G2, pigpio.OUTPUT)
flash_500=[] # flash every 500 ms
flash_100=[] # flash every 100 ms
# ON OFF DELAY
flash_500.append(pigpio.pulse(1<<G1, 1<<G2, 500000))
flash_500.append(pigpio.pulse(1<<G2, 1<<G1, 500000))
flash_100.append(pigpio.pulse(1<<G1, 1<<G2, 100000))
flash_100.append(pigpio.pulse(1<<G2, 1<<G1, 100000))
pi.wave_clear() # clear any existing waveforms
pi.wave_add_generic(flash_500) # 500 ms flashes
f500 = pi.wave_create() # create and save id
pi.wave_add_generic(flash_100) # 100 ms flashes
f100 = pi.wave_create() # create and save id
pi.wave_send_repeat(f500)
time.sleep(4)
pi.wave_send_repeat(f100)
time.sleep(4)
pi.wave_send_repeat(f500)
time.sleep(4)
pi.wave_tx_stop() # stop waveform
pi.wave_clear() # clear all waveforms
pi.wave_add_new()
pi.wave_add_serial(4, 300, 'Hello world')
pi.wave_add_serial(4, 300, b"Hello world")
pi.wave_add_serial(4, 300, b'\x23\x01\x00\x45')
pi.wave_add_serial(17, 38400, [23, 128, 234], 5000)
Name | Cmd & Data | Meaning |
Loop Start | 255 0 | Identify start of a wave block |
Loop Repeat | 255 1 x y | loop x + y*256 times |
Delay | 255 2 x y | delay x + y*256 microseconds |
Loop Forever | 255 3 | loop forever |
#!/usr/bin/env python
import time
import pigpio
WAVES=5
GPIO=4
wid=[0]*WAVES
pi = pigpio.pi() # Connect to local Pi.
pi.set_mode(GPIO, pigpio.OUTPUT);
for i in range(WAVES):
pi.wave_add_generic([
pigpio.pulse(1<<GPIO, 0, 20),
pigpio.pulse(0, 1<<GPIO, (i+1)*200)]);
wid[i] = pi.wave_create();
pi.wave_chain([
wid[4], wid[3], wid[2], # transmit waves 4+3+2
255, 0, # loop start
wid[0], wid[0], wid[0], # transmit waves 0+0+0
255, 0, # loop start
wid[0], wid[1], # transmit waves 0+1
255, 2, 0x88, 0x13, # delay 5000us
255, 1, 30, 0, # loop end (repeat 30 times)
255, 0, # loop start
wid[2], wid[3], wid[0], # transmit waves 2+3+0
wid[3], wid[1], wid[2], # transmit waves 3+1+2
255, 1, 10, 0, # loop end (repeat 10 times)
255, 1, 5, 0, # loop end (repeat 5 times)
wid[4], wid[4], wid[4], # transmit waves 4+4+4
255, 2, 0x20, 0x4E, # delay 20000us
wid[0], wid[0], wid[0], # transmit waves 0+0+0
])
while pi.wave_tx_busy():
time.sleep(0.1);
for i in range(WAVES):
pi.wave_delete(wid[i])
pi.stop()
pi.wave_clear()
wid = pi.wave_create()
percent: 0-100, size of waveform as percentage of maximum available.
wid = pi.wave_create_and_pad(50)
pi.wave_delete(6) # delete waveform with id 6
pi.wave_delete(0) # delete waveform with id 0
cbs = pi.wave_get_cbs()
cbs = pi.wave_get_max_cbs()
micros = pi.wave_get_max_micros()
pulses = pi.wave_get_max_pulses()
micros = pi.wave_get_micros()
pulses = pi.wave_get_pulses()
cbs = pi.wave_send_once(wid)
cbs = pi.wave_send_repeat(wid)
cbs = pi.wave_send_using_mode(wid, WAVE_MODE_REPEAT_SYNC)
wid = pi.wave_tx_at()
pi.wave_send_once(0) # send first waveform
while pi.wave_tx_busy(): # wait for waveform to be sent
time.sleep(0.1)
pi.wave_send_once(1) # send next waveform
pi.wave_send_repeat(3)
time.sleep(5)
pi.wave_tx_stop()
pi.set_mode(17, pigpio.OUTPUT)
pi.write(17,0)
print(pi.read(17))
0
pi.write(17,1)
print(pi.read(17))
1
print(pigpio.error_text(-5))
level not 0-1
print(pigpio.tickDiff(4294967272, 12))
36
print(u2i(4294967272))
-24
print(u2i(37))
37
22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
a a a a a a a - - IT HC TF IR RE TE BK EC ES PL PH I2 SP EN
Dutycycle | On time |
0 | Off |
range_ * 0.25 | 25% On |
range_ * 0.50 | 50% On |
range_ * 0.75 | 75% On |
range_ | Fully On |
EITHER_EDGE = 2
FALLING_EDGE = 1
RISING_EDGE = 0
PI_BAD_USER_GPIO = -2
PI_BAD_GPIO = -3
PI_BAD_MODE = -4
PI_BAD_LEVEL = -5
PI_BAD_PUD = -6
PI_BAD_PULSEWIDTH = -7
PI_BAD_DUTYCYCLE = -8
PI_BAD_WDOG_TIMEOUT = -15
PI_BAD_DUTYRANGE = -21
PI_NO_HANDLE = -24
PI_BAD_HANDLE = -25
PI_BAD_WAVE_BAUD = -35
PI_TOO_MANY_PULSES = -36
PI_TOO_MANY_CHARS = -37
PI_NOT_SERIAL_GPIO = -38
PI_NOT_PERMITTED = -41
PI_SOME_PERMITTED = -42
PI_BAD_WVSC_COMMND = -43
PI_BAD_WVSM_COMMND = -44
PI_BAD_WVSP_COMMND = -45
PI_BAD_PULSELEN = -46
PI_BAD_SCRIPT = -47
PI_BAD_SCRIPT_ID = -48
PI_BAD_SER_OFFSET = -49
PI_GPIO_IN_USE = -50
PI_BAD_SERIAL_COUNT = -51
PI_BAD_PARAM_NUM = -52
PI_DUP_TAG = -53
PI_TOO_MANY_TAGS = -54
PI_BAD_SCRIPT_CMD = -55
PI_BAD_VAR_NUM = -56
PI_NO_SCRIPT_ROOM = -57
PI_NO_MEMORY = -58
PI_SOCK_READ_FAILED = -59
PI_SOCK_WRIT_FAILED = -60
PI_TOO_MANY_PARAM = -61
PI_SCRIPT_NOT_READY = -62
PI_BAD_TAG = -63
PI_BAD_MICS_DELAY = -64
PI_BAD_MILS_DELAY = -65
PI_BAD_WAVE_ID = -66
PI_TOO_MANY_CBS = -67
PI_TOO_MANY_OOL = -68
PI_EMPTY_WAVEFORM = -69
PI_NO_WAVEFORM_ID = -70
PI_I2C_OPEN_FAILED = -71
PI_SER_OPEN_FAILED = -72
PI_SPI_OPEN_FAILED = -73
PI_BAD_I2C_BUS = -74
PI_BAD_I2C_ADDR = -75
PI_BAD_SPI_CHANNEL = -76
PI_BAD_FLAGS = -77
PI_BAD_SPI_SPEED = -78
PI_BAD_SER_DEVICE = -79
PI_BAD_SER_SPEED = -80
PI_BAD_PARAM = -81
PI_I2C_WRITE_FAILED = -82
PI_I2C_READ_FAILED = -83
PI_BAD_SPI_COUNT = -84
PI_SER_WRITE_FAILED = -85
PI_SER_READ_FAILED = -86
PI_SER_READ_NO_DATA = -87
PI_UNKNOWN_COMMAND = -88
PI_SPI_XFER_FAILED = -89
PI_NO_AUX_SPI = -91
PI_NOT_PWM_GPIO = -92
PI_NOT_SERVO_GPIO = -93
PI_NOT_HCLK_GPIO = -94
PI_NOT_HPWM_GPIO = -95
PI_BAD_HPWM_FREQ = -96
PI_BAD_HPWM_DUTY = -97
PI_BAD_HCLK_FREQ = -98
PI_BAD_HCLK_PASS = -99
PI_HPWM_ILLEGAL = -100
PI_BAD_DATABITS = -101
PI_BAD_STOPBITS = -102
PI_MSG_TOOBIG = -103
PI_BAD_MALLOC_MODE = -104
PI_BAD_SMBUS_CMD = -107
PI_NOT_I2C_GPIO = -108
PI_BAD_I2C_WLEN = -109
PI_BAD_I2C_RLEN = -110
PI_BAD_I2C_CMD = -111
PI_BAD_I2C_BAUD = -112
PI_CHAIN_LOOP_CNT = -113
PI_BAD_CHAIN_LOOP = -114
PI_CHAIN_COUNTER = -115
PI_BAD_CHAIN_CMD = -116
PI_BAD_CHAIN_DELAY = -117
PI_CHAIN_NESTING = -118
PI_CHAIN_TOO_BIG = -119
PI_DEPRECATED = -120
PI_BAD_SER_INVERT = -121
PI_BAD_FOREVER = -124
PI_BAD_FILTER = -125
PI_BAD_PAD = -126
PI_BAD_STRENGTH = -127
PI_FIL_OPEN_FAILED = -128
PI_BAD_FILE_MODE = -129
PI_BAD_FILE_FLAG = -130
PI_BAD_FILE_READ = -131
PI_BAD_FILE_WRITE = -132
PI_FILE_NOT_ROPEN = -133
PI_FILE_NOT_WOPEN = -134
PI_BAD_FILE_SEEK = -135
PI_NO_FILE_MATCH = -136
PI_NO_FILE_ACCESS = -137
PI_FILE_IS_A_DIR = -138
PI_BAD_SHELL_STATUS = -139
PI_BAD_SCRIPT_NAME = -140
PI_BAD_SPI_BAUD = -141
PI_NOT_SPI_GPIO = -142
PI_BAD_EVENT_ID = -143
PI_CMD_INTERRUPTED = -144
PI_NOT_ON_BCM2711 = -145
PI_ONLY_ON_BCM2711 = -146
FILE_READ 1
FILE_WRITE 2
FILE_RW 3
FILE_APPEND 4
FILE_CREATE 8
FILE_TRUNC 16
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Type 1 X X - - X - - X X X X X - - X X
Type 2 - - X X X - - X X X X X - - X X
Type 3 X X X X X X X X X X X X X X
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Type 1 - X X - - X X X X X - - - - - -
Type 2 - X X - - - X X X X - X X X X X
Type 3 X X X X X X X X X X X X - - - -
CLEAR = 0
HIGH = 1
LOW = 0
OFF = 0
ON = 1
SET = 1
TIMEOUT = 2 # only returned for a watchdog timeout
ALT0 = 4
ALT1 = 5
ALT2 = 6
ALT3 = 7
ALT4 = 3
ALT5 = 2
INPUT = 0
OUTPUT = 1
WAVE_MODE_ONE_SHOT = 0
WAVE_MODE_REPEAT = 1
WAVE_MODE_ONE_SHOT_SYNC = 2
WAVE_MODE_REPEAT_SYNC = 3
Pad | GPIO |
0 | 0-27 |
1 | 28-45 |
2 | 46-53 |
PUD_DOWN = 1
PUD_OFF = 0
PUD_UP = 2
FROM_START=0
FROM_CURRENT=1
FROM_END=2