PiGPIO API
PiGPIO.Callback
PiGPIO.CallbackThread
PiGPIO.CallbackThread
PiGPIO.Callback_ADT
PiGPIO.Pi
PiGPIO.Pulse
PiGPIO.SockLock
PiGPIO.WaitForEdge
PiGPIO.WaitForEdge
Base.run
PiGPIO._pigpio_command
PiGPIO._pigpio_command_ext
PiGPIO._tally
PiGPIO._u2i
PiGPIO.append
PiGPIO.bb_i2c_close
PiGPIO.bb_i2c_open
PiGPIO.bb_i2c_zip
PiGPIO.bb_serial_invert
PiGPIO.bb_serial_read
PiGPIO.bb_serial_read_close
PiGPIO.bb_serial_read_open
PiGPIO.callback
PiGPIO.cancel
PiGPIO.clear_bank_1
PiGPIO.clear_bank_2
PiGPIO.custom_1
PiGPIO.custom_2
PiGPIO.error_text
PiGPIO.func
PiGPIO.get_PWM_dutycycle
PiGPIO.get_PWM_frequency
PiGPIO.get_PWM_range
PiGPIO.get_PWM_real_range
PiGPIO.get_current_tick
PiGPIO.get_hardware_revision
PiGPIO.get_mode
PiGPIO.get_pigpio_version
PiGPIO.get_servo_pulsewidth
PiGPIO.hardware_PWM
PiGPIO.hardware_clock
PiGPIO.i2c_block_process_call
PiGPIO.i2c_close
PiGPIO.i2c_open
PiGPIO.i2c_process_call
PiGPIO.i2c_read_block_data
PiGPIO.i2c_read_byte
PiGPIO.i2c_read_byte_data
PiGPIO.i2c_read_device
PiGPIO.i2c_read_i2c_block_data
PiGPIO.i2c_read_word_data
PiGPIO.i2c_write_block_data
PiGPIO.i2c_write_byte
PiGPIO.i2c_write_byte_data
PiGPIO.i2c_write_device
PiGPIO.i2c_write_i2c_block_data
PiGPIO.i2c_write_quick
PiGPIO.i2c_write_word_data
PiGPIO.i2c_zip
PiGPIO.notify_begin
PiGPIO.notify_close
PiGPIO.notify_open
PiGPIO.notify_pause
PiGPIO.read
PiGPIO.read_bank_1
PiGPIO.read_bank_2
PiGPIO.remove
PiGPIO.reset_tally
PiGPIO.rxbuf
PiGPIO.serial_close
PiGPIO.serial_data_available
PiGPIO.serial_open
PiGPIO.serial_read
PiGPIO.serial_read_byte
PiGPIO.serial_write
PiGPIO.serial_write_byte
PiGPIO.set_PWM_dutycycle
PiGPIO.set_PWM_frequency
PiGPIO.set_PWM_range
PiGPIO.set_bank_1
PiGPIO.set_bank_2
PiGPIO.set_mode
PiGPIO.set_pull_up_down
PiGPIO.set_servo_pulsewidth
PiGPIO.set_watchdog
PiGPIO.spi_close
PiGPIO.spi_open
PiGPIO.spi_read
PiGPIO.spi_write
PiGPIO.spi_xfer
PiGPIO.stop
PiGPIO.stop
PiGPIO.tally
PiGPIO.tickDiff
PiGPIO.wait_for_edge
PiGPIO.wave_add_generic
PiGPIO.wave_add_new
PiGPIO.wave_add_serial
PiGPIO.wave_chain
PiGPIO.wave_clear
PiGPIO.wave_create
PiGPIO.wave_delete
PiGPIO.wave_get_cbs
PiGPIO.wave_get_max_cbs
PiGPIO.wave_get_max_micros
PiGPIO.wave_get_max_pulses
PiGPIO.wave_get_micros
PiGPIO.wave_get_pulses
PiGPIO.wave_send_once
PiGPIO.wave_send_repeat
PiGPIO.wave_send_using_mode
PiGPIO.wave_tx_at
PiGPIO.wave_tx_busy
PiGPIO.wave_tx_repeat
PiGPIO.wave_tx_start
PiGPIO.wave_tx_stop
PiGPIO.write
PiGPIO.Callback
— TypeA class to provide GPIO level change callbacks.
PiGPIO.CallbackThread
— TypeA class to encapsulate pigpio notification callbacks.
PiGPIO.CallbackThread
— MethodInitialises notifications.
PiGPIO.Callback_ADT
— TypeAn ADT class to hold callback information
gpio
: Broadcom GPIO number.edge
:PiGPIO.EITHER_EDGE
,PiGPIO.RISING_EDGE
, orPiGPIO.FALLING_EDGE
.func
: a user function taking three arguments (GPIO, level, tick).
PiGPIO.Pi
— MethodPi(; host = get(ENV, "PIGPIO_ADDR", ""),
port = get(ENV, "PIGPIO_PORT", 8888))
Grants access to a Pi's GPIO.
host
is the host name of the Pi on which the pigpio daemon is running. The default is localhost unless overridden by the PIGPIO_ADDR environment variable.
port
is the port number on which the pigpio daemon is listening. The default is 8888 unless overridden by the PIGPIO_PORT environment variable. The pigpio daemon must have been started with the same port number.
This connects to the pigpio daemon and reserves resources to be used for sending commands and receiving notifications.
An instance attribute connected
may be used to check the success of the connection. If the connection is established successfully connected
will be true, otherwise false.
pi = PiGPIO.Pi() # use defaults
pi = PiGPIO.Pi(host = "mypi") # specify host, default port
pi = PiGPIO.Pi(host = "mypi", port = 7777) # specify host and port
pi = PiGPIO.Pi() # exit script if no connection
if !pi.connected
exit()
end
PiGPIO.Pulse
— TypeA class to store pulse information.
gpio_on
: the GPIO to switch on at the start of the pulse. gpio_off
: the GPIO to switch off at the start of the pulse. delay
: the delay in microseconds before the next pulse.
PiGPIO.SockLock
— TypeA class to store socket and lock.
PiGPIO.WaitForEdge
— TypeEncapsulates waiting for GPIO edges.
PiGPIO.WaitForEdge
— MethodInitialises a waitforedge.
Base.run
— MethodRuns the notification thread.
PiGPIO._pigpio_command
— FunctionRuns a pigpio socket command.
sl
: command socket and lock.cmd
: the command to be executed.p1
: command parameter 1 (if applicable).p2
: command parameter 2 (if applicable).
PiGPIO._pigpio_command_ext
— FunctionRuns an extended pigpio socket command.
sl
: command socket and lock.cmd
: the command to be executed.p1
: command parameter 1 (if applicable).p2
: command parameter 2 (if applicable).p3
: total size in bytes of following extentsextents
: additional data blocks
PiGPIO._tally
— MethodIncrement the callback called count.
PiGPIO._u2i
— MethodConverts a 32 bit unsigned number to signed. If the number is negative it indicates an error. On error a pigpio exception will be raised if exceptions is true.
PiGPIO.append
— MethodAdds a callback to the notification thread.
PiGPIO.bb_i2c_close
— MethodThis function stops bit banging I2C on a pair of GPIO previously opened with bb_i2c_open
.
SDA
: 0-31, the SDA GPIO used in a prior call tobb_i2c_open
Returns 0 if OK, otherwise PiGPIO.PI_BAD_USER_GPIO
, or PiGPIO.PI_NOT_I2C_GPIO
.
bb_i2c_close(pi, SDA)
PiGPIO.bb_i2c_open
— FunctionThis function selects a pair of GPIO for bit banging I2C at a specified baud rate.
Bit banging I2C allows for certain operations which are not possible with the standard I2C driver.
o baud rates as low as 50 o repeated starts o clock stretching o I2C on any pair of spare GPIO
SDA
: 0-31SCL
: 0-31baud
: 50-500000
Returns 0 if OK, otherwise PiGPIO.PI_BAD_USER_GPIO
, PiGPIO.PI_BAD_I2C_BAUD
, or PiGPIO.PI_GPIO_IN_USE
.
The GPIO used for SDA and SCL must have pull-ups to 3V3 connected. As a guide the hardware pull-ups on pins 3 and 5 are 1k8 in value.
h = bb_i2c_open(pi, 4, 5, 50000) # bit bang on GPIO 4/5 at 50kbps
PiGPIO.bb_i2c_zip
— MethodThis function executes a sequence of bit banged I2C operations. The operations to be performed are specified by the contents of data which contains the concatenated command codes and associated data.
SDA
: 0-31 (as used in a prior call tobb_i2c_open
)data
: the concatenated I2C commands, see below
The returned value is a tuple of the number of bytes read and a bytearray containing the bytes. If there was an error the number of bytes read will be less than zero (and will contain the error code).
(count, data) = pi.bb_i2c_zip(
h, [4, 0x53, 2, 7, 1, 0x32, 2, 6, 6, 3, 0])
The following command codes are supported
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
The address, read, and write commands take a parameter P. Normally P is one byte (0-255). If the command is preceded by the Escape command then P is two bytes (0-65535, least significant byte first).
The address and flags default to 0. The address and flags maintain their previous value until updated.
No flags are currently defined.
Any read I2C data is concatenated in the returned bytearray.
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
PiGPIO.bb_serial_invert
— MethodInvert serial logic.
user_gpio
: 0-31 (opened in a prior call tobb_serial_read_open
)invert
: 0-1 (1 invert, 0 normal)
status = bb_serial_invert(pi, 17, 1)
PiGPIO.bb_serial_read
— MethodReturns data from the bit bang serial cyclic buffer.
user_gpio
: 0-31 (opened in a prior call tobb_serial_read_open
)
The returned value is a tuple of the number of bytes read and a bytearray containing the bytes. If there was an error the number of bytes read will be less than zero (and will contain the error code).
The bytes returned for each character depend upon the number of data bits bb_bits
specified in the bb_serial_read_open
command.
For bb_bits
1-8 there will be one byte per character. For bb_bits
9-16 there will be two bytes per character. For bb_bits
17-32 there will be four bytes per character.
(count, data) = bb_serial_read(pi, 4)
PiGPIO.bb_serial_read_close
— MethodCloses a GPIO for bit bang reading of serial data.
user_gpio
: 0-31 (opened in a prior call tobb_serial_read_open
)
status = bb_serial_read_close(pi, 17)
PiGPIO.bb_serial_read_open
— FunctionOpens a GPIO for bit bang reading of serial data.
user_gpio
: 0-31, the GPIO to use.baud
: 50-250000, the baud rate.bb_bits
: 1-32, the number of bits per word, default 8.
The serial data is held in a cyclic buffer and is read using bb_serial_read
.
It is the caller's responsibility to read data from the cyclic buffer in a timely fashion.
status = bb_serial_read_open(pi, 4, 19200)
status = bb_serial_read_open(pi, 17, 9600)
PiGPIO.callback
— FunctionPiGPIO.callback(self::Pi, user_gpio, edge=RISING_EDGE, func=nothing)
Calls a user supplied function (a callback) whenever the specified GPIO edge is detected.
user_gpio
: 0-31.edge
:PiGPIO.EITHER_EDGE
,PiGPIO.RISING_EDGE
(default), orPiGPIO.FALLING_EDGE
.func
: user supplied callback function.
The user supplied callback receives three parameters, the GPIO, the level, and the tick.
If a user callback is not specified a default tally callback is provided which simply counts edges. The count may be retrieved by calling the tally function. The count may be reset to zero by calling the reset_tally function.
The callback may be cancelled by calling the cancel function.
A GPIO may have multiple callbacks (although I can't think of a reason to do so).
function cbf(gpio, level, tick)
print(gpio, level, tick)
end
cb1 = callback(pi, 22, PiGPIO.EITHER_EDGE, cbf)
cb2 = callback(pi, 4, PiGPIO.EITHER_EDGE)
cb3 = callback(pi, 17)
print(cb3.tally())
cb3.reset_tally()
cb1.cancel() # To cancel callback cb1.
PiGPIO.cancel
— MethodCancels a callback by removing it from the notification thread.
PiGPIO.clear_bank_1
— MethodPiGPIO.clear_bank_1(self::Pi, bits)
Clears GPIO 0-31 if the corresponding bit in bits is set.
bits
is a 32 bit mask with 1 set if the corresponding GPIO is to be cleared.
A returned status of PiGPIO.PI_SOME_PERMITTED
indicates that the user is not allowed to write to one or more of the GPIO.
clear_bank_1(pi,0b111110010000)
PiGPIO.clear_bank_2
— MethodPiGPIO.clear_bank_2(self::Pi, bits)
Clears GPIO 32-53 if the corresponding bit (0-21) in bits is set.
bits
is a 32 bit mask with 1 set if the corresponding GPIO is to be cleared.
A returned status of PiGPIO.PI_SOME_PERMITTED
indicates that the user is not allowed to write to one or more of the GPIO.
clear_bank_2(pi, 0x1010)
PiGPIO.custom_1
— FunctionPiGPIO.custom_1(self, arg1=0, arg2=0, argx=[])
Calls a pigpio function customised by the user.
arg1
is >=0, default 0. arg2
is >=0, default 0. argx
is an extra arguments (each 0-255), default empty.
The returned value is an integer which by convention should be >=0 for OK and <0 for error.
value = PiGPIO.custom_1(pi)
value = PiGPIO.custom_1(pi, 23)
value = PiGPIO.custom_1(pi, 0, 55)
value = PiGPIO.custom_1(pi, 23, 56, [1, 5, 7])
value = PiGPIO.custom_1(pi, 23, 56, b"hello")
value = PiGPIO.custom_1(pi, 23, 56, "hello")
PiGPIO.custom_2
— FunctionPiGPIO.custom_2(self, arg1=0, argx=[], retMax=8192)
Calls a pigpio function customised by the user.
arg1
is >=0, default 0. argx
extra arguments (each 0-255), default empty. retMax
is >=0, maximum number of bytes to return, default 8192.
The returned value is a tuple of the number of bytes returned and a bytearray containing the bytes. If there was an error the number of bytes read will be less than zero (and will contain the error code).
(count, data) = PiGPIO.custom_2(pi)
(count, data) = PiGPIO.custom_2(pi, 23)
(count, data) = PiGPIO.custom_2(pi, 23, [1, 5, 7])
(count, data) = PiGPIO.custom_2(pi, 23, b"hello")
(count, data) = PiGPIO.custom_2(pi, 23, "hello", 128)
PiGPIO.error_text
— MethodPiGPIO.error_text(errnum)
Returns a text description of a PiGPIO error number. errnum
(errnum
<0).
print(PiGPIO.error_text(-5))
# output: level not 0-1
PiGPIO.func
— MethodSets waitforedge triggered.
PiGPIO.get_PWM_dutycycle
— MethodPiGPIO.get_PWM_dutycycle(self::Pi, user_gpio)
Returns the PWM dutycycle being used on the GPIO.
user_gpio
: 0-31.
For normal PWM the dutycycle will be out of the defined range for the GPIO (see get_PWM_range
).
If a hardware clock is active on the GPIO the reported dutycycle will be 500000 (500k) out of 1000000 (1M).
If hardware PWM is active on the GPIO the reported dutycycle will be out of a 1000000 (1M).
set_PWM_dutycycle(pi, 4, 25)
print(get_PWM_dutycycle(pi, 4))
# output 25
set_PWM_dutycycle(pi, 4, 203)
print(get_PWM_dutycycle(pi, 4))
# output 203
PiGPIO.get_PWM_frequency
— MethodReturns the frequency of PWM being used on the GPIO.
user_gpio
= 0-31.
Returns the frequency (in Hz) used for the GPIO.
For normal PWM the frequency will be that defined for the GPIO by set_PWM_frequency
.
If a hardware clock is active on the GPIO the reported frequency will be that set by hardware_clock
.
If hardware PWM is active on the GPIO the reported frequency will be that set by hardware_PWM
.
set_PWM_frequency(pi, 4,0)
print(get_PWM_frequency(pi, 4))
10
set_PWM_frequency(pi, 4, 800)
print(get_PWM_frequency(pi, 4))
800
PiGPIO.get_PWM_range
— MethodPiGPIO.get_PWM_range(self::Pi, user_gpio)
Returns the range of PWM values being used on the GPIO. user_gpio
is an integer between 0 and 31.
If a hardware clock or hardware PWM is active on the GPIO the reported range will be 1000000 (1M).
set_PWM_range(pi, 9, 500)
print(get_PWM_range(pi, 9))
# output 500
PiGPIO.get_PWM_real_range
— MethodPiGPIO.get_PWM_real_range(self::Pi, user_gpio)
Returns the real (underlying) range of PWM values being used on the GPIO.
user_gpio
: 0-31.
If a hardware clock is active on the GPIO the reported real range will be 1000000 (1M).
If hardware PWM is active on the GPIO the reported real range will be approximately 250M divided by the set PWM frequency.
set_PWM_frequency(pi, 4, 800)
print(get_PWM_real_range(pi, 4))
# output 250
PiGPIO.get_current_tick
— MethodPiGPIO.get_current_tick(self::Pi)
Returns the current system tick.
Tick is the number of microseconds since system boot. As an unsigned 32 bit quantity tick wraps around approximately every 71.6 minutes.
t1 = PiGPIO.get_current_tick(pi)
sleep(1)
t2 = PiGPIO.get_current_tick(pi)
PiGPIO.get_hardware_revision
— MethodPiGPIO.get_hardware_revision(self::Pi)
Returns the Pi's hardware revision number.
The hardware revision is the last few characters on the revision line of /proc/cpuinfo
.
The revision number can be used to determine the assignment of GPIO to pins (see gpio
).
There are at least three types of board.
Type 1 boards have hardware revision numbers of 2 and 3.
Type 2 boards have hardware revision numbers of 4, 5, 6, and 15.
Type 3 boards have hardware revision numbers of 16 or greater.
If the hardware revision can not be found or is not a valid hexadecimal number the function returns 0.
print(get_hardware_revision(pi))
2
PiGPIO.get_mode
— Methodget_mode(self::Pi, gpio)
Returns the GPIO mode for the pin gpio
(integer between 0 and 53).
Returns a value as follows:
0 = INPUT
1 = OUTPUT
2 = ALT5
3 = ALT4
4 = ALT0
5 = ALT1
6 = ALT2
7 = ALT3
print(get_mode(pi, 0))
# output 4
PiGPIO.get_pigpio_version
— MethodPiGPIO.get_pigpio_version(self::Pi)
Returns the pigpio software version.
v = get_pigpio_version(pi)
PiGPIO.get_servo_pulsewidth
— MethodReturns the servo pulsewidth being used on the GPIO.
user_gpio
: 0-31.
Returns the servo pulsewidth.
set_servo_pulsewidth(pi, 4, 525)
print(get_servo_pulsewidth(pi, 4))
525
set_servo_pulsewidth(pi, 4, 2130)
print(get_servo_pulsewidth(pi, 4))
2130
PiGPIO.hardware_PWM
— MethodPiGPIO.hardware_PWM(self::Pi, gpio, PWMfreq, PWMduty)
Starts hardware PWM on a GPIO at the specified frequency and dutycycle. Frequencies above 30MHz are unlikely to work.
Any waveform started by wave_send_once
, wave_send_repeat
, or wave_chain
will be cancelled.
This function is only valid if the pigpio main clock is PCM. The main clock defaults to PCM but may be overridden when the pigpio daemon is started (option -t).
gpio
: see descripton, PWMfreq
: 0 (off) or 1-125000000 (125M), PWMduty
: 0 (off) to 1000000 (1M)(fully on).
Returns 0 if OK, otherwise PiGPIO.PI_NOT_PERMITTED
, PiGPIO.PI_BAD_GPIO
, PiGPIO.PI_NOT_HPWM_GPIO
, PiGPIO.PI_BAD_HPWM_DUTY
, PiGPIO.PI_BAD_HPWM_FREQ
.
The same PWM channel is available on multiple GPIO. The latest frequency and dutycycle setting will be used by all GPIO which share a PWM channel.
The GPIO must be one of the following.
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
The actual number of steps beween off and fully on is the integral part of 250 million divided by PWMfreq.
The actual frequency set is 250 million / steps.
There will only be a million steps for a PWMfreq of 250. Lower frequencies will have more steps and higher frequencies will have fewer steps. PWMduty is automatically scaled to take this into account.
PiGPIO.hardware_PWM(pi, 18, 800, 250000) # 800Hz 25% dutycycle
PiGPIO.hardware_PWM(pi, 18, 2000, 750000) # 2000Hz 75% dutycycle
PiGPIO.hardware_clock
— MethodPiGPIO.hardware_clock(self::Pi, gpio, clkfreq)
Starts a hardware clock on a GPIO at the specified frequency. Frequencies above 30MHz are unlikely to work. See description below for the gpio
parameter. clkfreq
is 0 (off) or 4689-250000000 (250M)
Returns 0 if OK, otherwise PiGPIO.PI_NOT_PERMITTED
, PiGPIO.PI_BAD_GPIO
, PiGPIO.PI_NOT_HCLK_GPIO
, PiGPIO.PI_BAD_HCLK_FREQ
, or PiGPIO.PI_BAD_HCLK_PASS
.
The same clock is available on multiple GPIO. The latest frequency setting will be used by all GPIO which share a clock.
The GPIO must be one of the following.
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)
Access to clock 1 is protected by a password as its use will likely crash the Pi. The password is given by or'ing 0x5A000000 with the GPIO number.
hardware_clock(pi, 4, 5000) # 5 KHz clock on GPIO 4
hardware_clock(pi, 4, 40000000) # 40 MHz clock on GPIO 4
PiGPIO.i2c_block_process_call
— MethodWrites data bytes to the specified register of the device associated with handle and reads a device specified number of bytes of data in return.
handle
: >=0 (as returned by a prior call toi2c_open
).reg
: >=0, the device register.data
: the bytes to write.
The SMBus 2.0 documentation states that a minimum of 1 byte may be sent and a minimum of 1 byte may be received. The total number of bytes sent/received must be 32 or less.
SMBus 2.0 5.5.8 - Block write-block read.
S Addr Wr [A] reg [A] length(data) [A] data0 [A] ... datan [A]
S Addr Rd [A] [Count] A [Data] ... A P
The returned value is a tuple of the number of bytes read and a bytearray containing the bytes. If there was an error the number of bytes read will be less than zero (and will contain the error code).
(b, d) = i2c_block_process_call(pi, h, 10, b'\x02\x05\x00')
(b, d) = i2c_block_process_call(pi, h, 10, b'abcdr')
(b, d) = i2c_block_process_call(pi, h, 10, "abracad")
(b, d) = i2c_block_process_call(pi, h, 10, [2, 5, 16])
PiGPIO.i2c_close
— MethodCloses the I2C device associated with handle.
handle
: >=0 (as returned by a prior call toi2c_open
).
i2c_close(pi, h)
PiGPIO.i2c_open
— FunctionReturns a handle (>=0) for the device at the I2C bus address.
i2c_bus
: >=0.i2c_address
: 0-0x7F.i2c_flags
: 0, no flags are currently defined.
Normally you would only use the i2c_*
functions if you are or will be connecting to the Pi over a network. If you will always run on the local Pi use the standard SMBus module instead.
Physically buses 0 and 1 are available on the Pi. Higher numbered buses will be available if a kernel supported bus multiplexor is being used.
For the SMBus commands the low level transactions are shown at the end of the function description. The following abbreviations are used.
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 = i2c_open(pi, 1, 0x53) # open device at address 0x53 on bus 1
PiGPIO.i2c_process_call
— MethodWrites 16 bits of data to the specified register of the device associated with handle and reads 16 bits of data in return.
handle
: >=0 (as returned by a prior call toi2c_open
).reg
: >=0, the device register.word_val
: 0-65535, the value to write.
SMBus 2.0 5.5.6 - Process call.
S Addr Wr [A] reg [A] word_val_Low [A] word_val_High [A]
S Addr Rd [A] [DataLow] A [DataHigh] NA P
r = i2c_process_call(pi, h, 4, 0x1231)
r = i2c_process_call(pi, h, 6, 0)
PiGPIO.i2c_read_block_data
— MethodReads a block of up to 32 bytes from the specified register of the device associated with handle.
handle
: >=0 (as returned by a prior call toi2c_open
).reg
: >=0, the device register.
SMBus 2.0 5.5.7 - Block read.
S Addr Wr [A] reg [A]
S Addr Rd [A] [Count] A [Data] A [Data] A ... A [Data] NA P
The amount of returned data is set by the device.
The returned value is a tuple of the number of bytes read and a bytearray containing the bytes. If there was an error the number of bytes read will be less than zero (and will contain the error code).
(b, d) = i2c_read_block_data(pi, h, 10)
if b >= 0
# process data
else
# process read failure
PiGPIO.i2c_read_byte
— MethodReads a single byte from the device associated with handle.
handle
: >=0 (as returned by a prior call toi2c_open
).
SMBus 2.0 5.5.3 - Receive byte.
S Addr Rd [A] [Data] NA P
b = i2c_read_byte(pi, 2) # read a byte from device 2
PiGPIO.i2c_read_byte_data
— MethodReads a single byte from the specified register of the device associated with handle.
handle
: >=0 (as returned by a prior call toi2c_open
).reg
: >=0, the device register.
SMBus 2.0 5.5.5 - Read byte.
S Addr Wr [A] reg [A] S Addr Rd [A] [Data] NA P
# read byte from reg 17 of device 2
b = i2c_read_byte_data(pi, 2, 17)
# read byte from reg 1 of device 0
b = i2c_read_byte_data(pi, 0, 1)
PiGPIO.i2c_read_device
— MethodReturns count bytes read from the raw device associated with handle.
handle
: >=0 (as returned by a prior call toi2c_open
).count
: >0, the number of bytes to read.
S Addr Rd [A] [Data] A [Data] A ... A [Data] NA P
The returned value is a tuple of the number of bytes read and a bytearray containing the bytes. If there was an error the number of bytes read will be less than zero (and will contain the error code).
(count, data) = i2c_read_device(pi, h, 12)
PiGPIO.i2c_read_i2c_block_data
— MethodReads count bytes from the specified register of the device associated with handle . The count may be 1-32.
handle
: >=0 (as returned by a prior call toi2c_open
).reg
: >=0, the device register.count
: >0, the number of bytes to read.
S Addr Wr [A] reg [A]
S Addr Rd [A] [Data] A [Data] A ... A [Data] NA P
The returned value is a tuple of the number of bytes read and a bytearray containing the bytes. If there was an error the number of bytes read will be less than zero (and will contain the error code).
(b, d) = i2c_read_i2c_block_data(pi, h, 4, 32)
if b >= 0
# process data
else
# process read failure
PiGPIO.i2c_read_word_data
— MethodReads a single 16 bit word from the specified register of the device associated with handle.
handle
: >=0 (as returned by a prior call toi2c_open
).reg
: >=0, the device register.
SMBus 2.0 5.5.5 - Read word.
S Addr Wr [A] reg [A] S Addr Rd [A] [DataLow] A [DataHigh] NA P
# read word from reg 2 of device 3
w = i2c_read_word_data(pi, 3, 2)
# read word from reg 7 of device 2
w = i2c_read_word_data(pi, 2, 7)
PiGPIO.i2c_write_block_data
— MethodWrites up to 32 bytes to the specified register of the device associated with handle.
handle
: >=0 (as returned by a prior call toi2c_open
).reg
: >=0, the device register.data
: the bytes to write.
SMBus 2.0 5.5.7 - Block write.
S Addr Wr [A] reg [A] length(data) [A] data0 [A] data1 [A] ... [A]
datan [A] P
i2c_write_block_data(pi, 4, 5, b'hello')
i2c_write_block_data(pi, 4, 5, "data bytes")
i2c_write_block_data(pi, 5, 0, b'\x00\x01\x22')
i2c_write_block_data(pi, 6, 2, [0, 1, 0x22])
PiGPIO.i2c_write_byte
— MethodSends a single byte to the device associated with handle.
handle
: >=0 (as returned by a prior call toi2c_open
).byte_val
: 0-255, the value to write.
SMBus 2.0 5.5.2 - Send byte.
S Addr Wr [A] byte_val [A] P
i2c_write_byte(pi, 1, 17) # send byte 17 to device 1
i2c_write_byte(pi, 2, 0x23) # send byte 0x23 to device 2
PiGPIO.i2c_write_byte_data
— MethodWrites a single byte to the specified register of the device associated with handle.
handle
: >=0 (as returned by a prior call toi2c_open
).reg
: >=0, the device register.byte_val
: 0-255, the value to write.
SMBus 2.0 5.5.4 - Write byte.
S Addr Wr [A] reg [A] byte_val [A] P
# send byte 0xC5 to reg 2 of device 1
i2c_write_byte_data(pi, 1, 2, 0xC5)
# send byte 9 to reg 4 of device 2
i2c_write_byte_data(pi, 2, 4, 9)
PiGPIO.i2c_write_device
— MethodWrites the data bytes to the raw device associated with handle.
handle
: >=0 (as returned by a prior call toi2c_open
).data
: the bytes to write.
S Addr Wr [A] data0 [A] data1 [A] ... [A] datan [A] P
i2c_write_device(pi, h, b"\x12\x34\xA8")
i2c_write_device(pi, h, b"help")
i2c_write_device(pi, h, 'help')
i2c_write_device(pi, h, [23, 56, 231])
PiGPIO.i2c_write_i2c_block_data
— MethodWrites data bytes to the specified register of the device associated with handle . 1-32 bytes may be written.
handle
: >=0 (as returned by a prior call toi2c_open
).reg
: >=0, the device register.data
: the bytes to write.
S Addr Wr [A] reg [A] data0 [A] data1 [A] ... [A] datan [NA] P
i2c_write_i2c_block_data(pi, 4, 5, 'hello')
i2c_write_i2c_block_data(pi, 4, 5, b'hello')
i2c_write_i2c_block_data(pi, 5, 0, b'\x00\x01\x22')
i2c_write_i2c_block_data(pi, 6, 2, [0, 1, 0x22])
PiGPIO.i2c_write_quick
— MethodSends a single bit to the device associated with handle.
handle
: >=0 (as returned by a prior call toi2c_open
).bit
: 0 or 1, the value to write.
SMBus 2.0 5.5.1 - Quick command.
S Addr bit [A] P
i2c_write_quick(pi, 0, 1) # send 1 to device 0
i2c_write_quick(pi, 3, 0) # send 0 to device 3
PiGPIO.i2c_write_word_data
— MethodWrites a single 16 bit word to the specified register of the device associated with handle.
handle
: >=0 (as returned by a prior call toi2c_open
).reg
: >=0, the device register.word_val
: 0-65535, the value to write.
SMBus 2.0 5.5.4 - Write word.
S Addr Wr [A] reg [A] word_val_Low [A] word_val_High [A] P
# send word 0xA0C5 to reg 5 of device 4
i2c_write_word_data(pi, 4, 5, 0xA0C5)
# send word 2 to reg 2 of device 5
i2c_write_word_data(pi, 5, 2, 23)
PiGPIO.i2c_zip
— MethodThis function executes a sequence of I2C operations. The operations to be performed are specified by the contents of data which contains the concatenated command codes and associated data.
handle
: >=0 (as returned by a prior call toi2c_open
).data
: the concatenated I2C commands, see below
The returned value is a tuple of the number of bytes read and a bytearray containing the bytes. If there was an error the number of bytes read will be less than zero (and will contain the error code).
(count, data) = i2c_zip(pi, h, [4, 0x53, 7, 1, 0x32, 6, 6, 0])
The following command codes are supported
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
The address, read, and write commands take a parameter P. Normally P is one byte (0-255). If the command is preceded by the Escape command then P is two bytes (0-65535, least significant byte first).
The address defaults to that associated with the handle. The flags default to 0. The address and flags maintain their previous value until updated.
Any read I2C data is concatenated in the returned bytearray.
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
PiGPIO.notify_begin
— MethodStarts notifications on a handle.
handle
: >=0 (as returned by a prior call tonotify_open
)bits
: a 32 bit mask indicating the GPIO to be notified.
The notification sends state changes for each GPIO whose corresponding bit in bits is set.
The following code starts notifications for GPIO 1, 4, 6, 7, and 10 (1234 = 0x04D2 = 0b0000010011010010).
h = notify_open(pi)
if h >= 0
notify_begin(pi, h, 1234)
PiGPIO.notify_close
— MethodStops notifications on a handle and releases the handle for reuse.
handle
: >=0 (as returned by a prior call tonotify_open
)
h = notify_open(pi)
if h >= 0
notify_begin(pi, h, 1234)
# ...
notify_close(pi, h)
# ...
PiGPIO.notify_open
— MethodPiGPIO.notify_open(self::Pi)
Returns a notification handle (>=0).
A notification is a method for being notified of GPIO state changes via a pipe.
Pipes are only accessible from the local machine so this function serves no purpose if you are using Python from a remote machine. The in-built (socket) notifications provided by callback
should be used instead.
Notifications for handle x
will be available at the pipe named /dev/pigpiox
(where x
is the handle number).
E.g. if the function returns 15 then the notifications must be read from /dev/pigpio15
.
Notifications have the following structure.
I seqno
I flags
I tick
I level
seqno: starts at 0 each time the handle is opened and then increments by one for each report.
flags: two flags are defined, PI_NTFY_FLAGS_WDOG
and PI_NTFY_FLAGS_ALIVE
. If bit 5 is set (PI_NTFY_FLAGS_WDOG
) then bits 0-4 of the flags indicate a GPIO which has had a watchdog timeout; if bit 6 is set (PI_NTFY_FLAGS_ALIVE
) this indicates a keep alive signal on the pipe/socket and is sent once a minute in the absence of other notification activity.
tick: the number of microseconds since system boot. It wraps around after 1h12m.
level: indicates the level of each GPIO. If bit 1<<x is set then GPIO x is high.
h = notify_open(pi)
if h >= 0
notify_begin(pi, h, 1234)
PiGPIO.notify_pause
— MethodPauses notifications on a handle.
handle
: >=0 (as returned by a prior call tonotify_open
)
Notifications for the handle are suspended until notify_begin
is called again.
h = notify_open(pi)
if h >= 0
notify_begin(pi, h, 1234)
# ...
notify_pause(pi, h)
# ...
notify_begin(pi, h, 1234)
# ...
PiGPIO.read
— Methodread(self::Pi, gpio)
Returns the GPIO level for the pin gpio
(an integer between 0 and 53).
set_mode(pi, 23, PiGPIO.INPUT)
set_pull_up_down(pi, 23, PiGPIO.PUD_DOWN)
print(read(pi, 23))
# output 0
set_pull_up_down(pi, 23, PiGPIO.PUD_UP)
print(read(pi, 23))
# output 1
PiGPIO.read_bank_1
— MethodReturns the levels of the bank 1 GPIO (GPIO 0-31).
The returned 32 bit integer has a bit set if the corresponding GPIO is high. GPIO n has bit value (1<<n).
print(bin(read_bank_1(pi)))
0b10010100000011100100001001111
PiGPIO.read_bank_2
— MethodReturns the levels of the bank 2 GPIO (GPIO 32-53).
The returned 32 bit integer has a bit set if the corresponding GPIO is high. GPIO n has bit value (1<<(n-32)).
print(bin(read_bank_2(pi)))
0b1111110000000000000000
PiGPIO.remove
— MethodRemoves a callback from the notification thread.
PiGPIO.reset_tally
— MethodResets the tally count to zero.
PiGPIO.rxbuf
— MethodReturns count bytes from the command socket.
PiGPIO.serial_close
— MethodCloses the serial device associated with handle.
handle
: >=0 (as returned by a prior call toserial_open
).
serial_close(pi, h1)
PiGPIO.serial_data_available
— MethodReturns the number of bytes available to be read from the device associated with handle.
handle
: >=0 (as returned by a prior call toserial_open
).
rdy = serial_data_available(pi, h1)
if rdy > 0
(b, d) = serial_read(pi, h1, rdy)
PiGPIO.serial_open
— FunctionReturns a handle for the serial tty device opened at baud bits per second.
tty
: the serial device to open.baud
: baud rate in bits per second, see below.ser_flags
: 0, no flags are currently defined.
Normally you would only use the serial_*
functions if you are or will be connecting to the Pi over a network. If you will always run on the local Pi use the standard serial module instead.
The baud rate must be one of 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 9600, 19200, 38400, 57600, 115200, or 230400.
h1 = serial_open(pi, "/dev/ttyAMA0", 300)
h2 = serial_open(pi, "/dev/ttyUSB1", 19200, 0)
PiGPIO.serial_read
— MethodReads up to count bytes from the device associated with handle.
handle
: >=0 (as returned by a prior call toserial_open
).count
: >0, the number of bytes to read.
The returned value is a tuple of the number of bytes read and a bytearray containing the bytes. If there was an error the number of bytes read will be less than zero (and will contain the error code).
(b, d) = serial_read(pi, h2, 100)
if b > 0
# process read data
PiGPIO.serial_read_byte
— MethodReturns a single byte from the device associated with handle.
handle
: >=0 (as returned by a prior call toserial_open
).
b = serial_read_byte(pi, h1)
PiGPIO.serial_write
— MethodWrites the data bytes to the device associated with handle.
handle
: >=0 (as returned by a prior call toserial_open
).data
: the bytes to write.
serial_write(pi, h1, b'\x02\x03\x04')
serial_write(pi, h2, b'help')
serial_write(pi, h2, "hello")
serial_write(pi, h1, [2, 3, 4])
PiGPIO.serial_write_byte
— MethodWrites a single byte to the device associated with handle.
handle
: >=0 (as returned by a prior call toserial_open
).byte_val
: 0-255, the value to write.
serial_write_byte(pi, h1, 23)
serial_write_byte(h1, ord(pi, 'Z'))
PiGPIO.set_PWM_dutycycle
— MethodPiGPIO.set_PWM_dutycycle(self::Pi, user_gpio, dutycycle)
Starts (non-zero dutycycle) or stops (0) PWM pulses on the GPIO.
user_gpio
: 0-31.dutycycle
: 0-range (range defaults to 255).
The set_PWM_range
function can change the default range of 255.
set_PWM_dutycycle(pi, 4, 0) # PWM off
set_PWM_dutycycle(pi, 4, 64) # PWM 1/4 on
set_PWM_dutycycle(pi, 4, 128) # PWM 1/2 on
set_PWM_dutycycle(pi, 4, 192) # PWM 3/4 on
set_PWM_dutycycle(pi, 4, 255) # PWM full on
PiGPIO.set_PWM_frequency
— MethodSets the frequency (in Hz) of the PWM to be used on the GPIO.
user_gpio
: 0-31.frequency
: >=0 Hz
Returns the numerically closest frequency if OK, otherwise PIBADUSERGPIO or PINOT_PERMITTED.
If PWM is currently active on the GPIO it will be switched off and then back on at the new frequency.
Each GPIO can be independently set to one of 18 different PWM frequencies.
The selectable frequencies depend upon the sample rate which may be 1, 2, 4, 5, 8, or 10 microseconds (default 5). The sample rate is set when the pigpio daemon is started.
The frequencies for each sample rate are
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
set_PWM_frequency(pi, 4,0)
print(get_PWM_frequency(pi, 4))
10
set_PWM_frequency(pi, 4,100000)
print(get_PWM_frequency(pi, 4))
8000
PiGPIO.set_PWM_range
— MethodPiGPIO.set_PWM_range(self::Pi, user_gpio, range_)
Sets the range of PWM values to be used on the GPIO. user_gpio
is an integer between 0 and 31 and range_
is between 25 and 40000.
set_PWM_range(pi, 9, 100) # now 25 1/4, 50 1/2, 75 3/4 on
set_PWM_range(pi, 9, 500) # now 125 1/4, 250 1/2, 375 3/4 on
set_PWM_range(pi, 9, 3000) # now 750 1/4, 1500 1/2, 2250 3/4 on
PiGPIO.set_bank_1
— MethodPiGPIO.set_bank_1(self::Pi, bits)
Sets GPIO 0-31 if the corresponding bit in bits is set.
bits
is a 32 bit mask with 1 set if the corresponding GPIO is to be set.
A returned status of PiGPIO.PI_SOME_PERMITTED
indicates that the user is not allowed to write to one or more of the GPIO.
set_bank_1(pi, 0b111110010000)
PiGPIO.set_bank_2
— MethodSets GPIO 32-53 if the corresponding bit (0-21) in bits is set.
bits
: a 32 bit mask with 1 set if the corresponding GPIO is
to be set.
A returned status of PiGPIO.PI_SOME_PERMITTED
indicates that the user is not allowed to write to one or more of the GPIO.
set_bank_2(pi, 0x303)
PiGPIO.set_mode
— Methodset_mode(pi::Pi, pin::Int, mode)
Sets the GPIO mode
of the given pin
(integer between 0 and 53) of the Pi instance pi
. The mode con be PiGPIO.INPUT
, PiGPIO.OUTPUT
, PiGPIO.ALT0
, PiGPIO.ALT1
, PiGPIO.ALT2
, PiGPIO.ALT3
, PiGPIO.ALT4
or PiGPIO.ALT5
.
set_mode(pi, 4, PiGPIO.INPUT) # GPIO 4 as input
set_mode(pi, 17, PiGPIO.OUTPUT) # GPIO 17 as output
set_mode(pi, 24, PiGPIO.ALT2) # GPIO 24 as ALT2
PiGPIO.set_pull_up_down
— Methodset_pull_up_down(self::Pi, gpio, pud)
Sets or clears the internal GPIO pull-up/down resistor for the pin gpio
(integer between 0 and 53). Possible values for pud
are PiGPIO.PUD_UP
, PiGPIO.PUD_DOWN
or PiGPIO.PUD_OFF
.
set_pull_up_down(pi, 17, PiGPIO.PUD_OFF)
set_pull_up_down(pi, 23, PiGPIO.PUD_UP)
set_pull_up_down(pi, 24, PiGPIO.PUD_DOWN)
PiGPIO.set_servo_pulsewidth
— MethodStarts (500-2500) or stops (0) servo pulses on the GPIO.
user_gpio
: 0-31.pulsewidth
: 0 (off), 500 (most anti-clockwise) - 2500 (most clockwise).
The selected pulsewidth will continue to be transmitted until changed by a subsequent call to setservopulsewidth.
The pulsewidths supported by servos varies and should probably be determined by experiment. A value of 1500 should always be safe and represents the mid-point of rotation.
You can DAMAGE a servo if you command it to move beyond its limits.
set_servo_pulsewidth(pi, 17, 0) # off
set_servo_pulsewidth(pi, 17, 1000) # safe anti-clockwise
set_servo_pulsewidth(pi, 17, 1500) # centre
set_servo_pulsewidth(pi, 17, 2000) # safe clockwise
PiGPIO.set_watchdog
— MethodSets a watchdog timeout for a GPIO.
user_gpio
: 0-31.wdog_timeout
: 0-60000.
The watchdog is nominally in milliseconds.
Only one watchdog may be registered per GPIO.
The watchdog may be cancelled by setting timeout to 0.
If no level change has been detected for the GPIO for timeout milliseconds any notification for the GPIO has a report written to the fifo with the flags set to indicate a watchdog timeout.
The callback class interprets the flags and will call registered callbacks for the GPIO with level TIMEOUT.
set_watchdog(pi, 23, 1000) # 1000 ms watchdog on GPIO 23
set_watchdog(pi, 23, 0) # cancel watchdog on GPIO 23
PiGPIO.spi_close
— MethodCloses the SPI device associated with handle.
handle
: >=0 (as returned by a prior call tospi_open
).
spi_close(pi, h)
PiGPIO.spi_open
— FunctionReturns a handle for the SPI device on channel. Data will be transferred at baud bits per second. The flags may be used to modify the default behaviour of 4-wire operation, mode 0, active low chip select.
An auxiliary SPI device is available on all models but the A and B and may be selected by setting the A bit in the flags. The auxiliary device has 3 chip selects and a selectable word size in bits.
spi_channel
: 0-1 (0-2 for the auxiliary SPI device).baud
: 32K-125M (values above 30M are unlikely to work).spi_flags
: see below.
Normally you would only use the spi_*
functions if you are or will be connecting to the Pi over a network. If you will always run on the local Pi use the standard SPI module instead.
spi_flags consists of the least significant 22 bits.
. . 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 . .
mm defines the SPI mode.
modes 1 and 3 do not appear to work on the auxiliary device.
. . Mode POL PHA 0 0 0 1 0 1 2 1 0 3 1 1 . .
px is 0 if CEx is active low (default) and 1 for active high.
ux is 0 if the CEx GPIO is reserved for SPI (default) and 1 otherwise.
A is 0 for the standard SPI device, 1 for the auxiliary SPI.
W is 0 if the device is not 3-wire, 1 if the device is 3-wire. Standard SPI device only.
nnnn defines the number of bytes (0-15) to write before switching the MOSI line to MISO to read data. This field is ignored if W is not set. Standard SPI device only.
T is 1 if the least significant bit is transmitted on MOSI first, the default (0) shifts the most significant bit out first. Auxiliary SPI device only.
R is 1 if the least significant bit is received on MISO first, the default (0) receives the most significant bit first. Auxiliary SPI device only.
bbbbbb defines the word size in bits (0-32). The default (0) sets 8 bits per word. Auxiliary SPI device only.
The spi_read
, spi_write
, and spi_xfer
functions transfer data packed into 1, 2, or 4 bytes according to the word size in bits.
For bits 1-8 there will be one byte per character. For bits 9-16 there will be two bytes per character. For bits 17-32 there will be four bytes per character.
E.g. 32 12-bit words will be transferred in 64 bytes.
The other bits in flags should be set to zero.
# open SPI device on channel 1 in mode 3 at 50000 bits per second
h = spi_open(pi, 1, 50000, 3)
PiGPIO.spi_read
— MethodReads count bytes from the SPI device associated with handle.
handle
: >=0 (as returned by a prior call tospi_open
).count
: >0, the number of bytes to read.
The returned value is a tuple of the number of bytes read and a bytearray containing the bytes. If there was an error the number of bytes read will be less than zero (and will contain the error code).
(b, d) = spi_read(pi, h, 60) # read 60 bytes from device h
if b == 60
# process read data
else
# error path
PiGPIO.spi_write
— MethodWrites the data bytes to the SPI device associated with handle.
handle
: >=0 (as returned by a prior call tospi_open
).data
: the bytes to write.
spi_write(pi, 0, b'\x02\xc0\x80') # write 3 bytes to device 0
spi_write(pi, 0, b'defgh') # write 5 bytes to device 0
spi_write(pi, 0, "def") # write 3 bytes to device 0
spi_write(pi, 1, [2, 192, 128]) # write 3 bytes to device 1
PiGPIO.spi_xfer
— MethodWrites the data bytes to the SPI device associated with handle, returning the data bytes read from the device.
handle
: >=0 (as returned by a prior call tospi_open
).data
: the bytes to write.
The returned value is a tuple of the number of bytes read and a bytearray containing the bytes. If there was an error the number of bytes read will be less than zero (and will contain the error code).
(count, rx_data) = spi_xfer(pi, h, b'\x01\x80\x00')
(count, rx_data) = spi_xfer(pi, h, [1, 128, 0])
(count, rx_data) = spi_xfer(pi, h, b"hello")
(count, rx_data) = spi_xfer(pi, h, "hello")
PiGPIO.stop
— MethodPiGPIO.stop(self::CallbackThread)
Stops notifications.
PiGPIO.stop
— MethodPiGPIO.stop(self::Pi)
Release pigpio resources.
PiGPIO.stop(pi)
PiGPIO.tally
— MethodProvides a count of how many times the default tally callback has triggered.
The count will be zero if the user has supplied their own callback function.
PiGPIO.tickDiff
— MethodPiGPIO.tickDiff(t1,t2)
Returns the microsecond difference between two ticks t1
(the earlier tick) and t2
the later tick. If t2 - t1 < 0
, it is assumed that the time counter wrapped around the Int32 limit.
print(PiGPIO.tickDiff(4294967272, 12))
# output 36
PiGPIO.wait_for_edge
— FunctionWait for an edge event on a GPIO.
user_gpio
: 0-31.edge
:PiGPIO.EITHER_EDGE
,PiGPIO.RISING_EDGE
(default), orPiGPIO.FALLING_EDGE
.wait_timeout
: >=0.0 (default 60.0).
The function returns when the edge is detected or after the number of seconds specified by timeout has expired.
Do not use this function for precise timing purposes, the edge is only checked 20 times a second. Whenever you need to know the accurate time of GPIO events use a callback
function.
The function returns true if the edge is detected, otherwise false.
if wait_for_edge(pi, 23)
print("Rising edge detected")
else
print("wait for edge timed out")
if wait_for_edge(pi, 23, PiGPIO.FALLING_EDGE, 5.0)
print("Falling edge detected")
else
print("wait for falling edge timed out")
PiGPIO.wave_add_generic
— MethodAdds a list of pulses to the current waveform.
pulses
: list of pulses to add to the waveform.
Returns the new total number of pulses in the current waveform.
The pulses are interleaved in time order within the existing waveform (if any).
Merging allows the waveform to be built in parts, that is the settings for GPIO#1 can be added, and then GPIO#2 etc.
If the added waveform is intended to start after or within the existing waveform then the first pulse should consist solely of a delay.
G1=4
G2=24
set_mode(pi, G1, pigpio.OUTPUT)
set_mode(pi, 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))
wave_clear(pi, ) # clear any existing waveforms
wave_add_generic(pi, flash_500) # 500 ms flashes
f500 = wave_create(pi, ) # create and save id
wave_add_generic(pi, flash_100) # 100 ms flashes
f100 = wave_create(pi, ) # create and save id
wave_send_repeat(pi, f500)
time.sleep(4)
wave_send_repeat(pi, f100)
time.sleep(4)
wave_send_repeat(pi, f500)
time.sleep(4)
wave_tx_stop(pi, ) # stop waveform
wave_clear(pi, ) # clear all waveforms
PiGPIO.wave_add_new
— MethodStarts a new empty waveform.
You would not normally need to call this function as it is automatically called after a waveform is created with the wave_create
function.
wave_add_new(pi, )
PiGPIO.wave_add_serial
— FunctionAdds a waveform representing serial data to the existing waveform (if any). The serial data starts offset
microseconds from the start of the waveform.
user_gpio
: GPIO to transmit data. You must set the GPIO mode to output.baud
: 50-1000000 bits per second.data
: the bytes to write.offset
: number of microseconds from the start of the waveform, default 0.bb_bits
: number of data bits, default 8.bb_stop
: number of stop half bits, default 2.
Returns the new total number of pulses in the current waveform.
The serial data is formatted as one start bit, bb_bits
data bits, and bb_stop
/2 stop bits.
It is legal to add serial data streams with different baud rates to the same waveform.
The bytes required for each character depend upon bb_bits
.
For bb_bits
1-8 there will be one byte per character. For bb_bits
9-16 there will be two bytes per character. For bb_bits
17-32 there will be four bytes per character.
wave_add_serial(pi, 4, 300, 'Hello world')
wave_add_serial(pi, 4, 300, b"Hello world")
wave_add_serial(pi, 4, 300, b'\x23\x01\x00\x45')
wave_add_serial(pi, 17, 38400, [23, 128, 234], 5000)
PiGPIO.wave_chain
— MethodThis function transmits a chain of waveforms.
Any hardware PWM started by hardware_PWM
will be cancelled.
The waves to be transmitted are specified by the contents of data which contains an ordered list of wave_id
s and optional command codes and related data.
Returns 0 if OK, otherwise PICHAINNESTING, PICHAINLOOPCNT, PIBADCHAINLOOP, PIBADCHAINCMD, PICHAINCOUNTER, PIBADCHAINDELAY, PICHAINTOOBIG, or PIBADWAVEID.
Each wave is transmitted in the order specified. A wave may occur multiple times per chain.
A blocks of waves may be transmitted multiple times by using the loop commands. The block is bracketed by loop start and end commands. Loops may be nested.
Delays between waves may be added with the delay command.
The following command codes are supported
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
If present Loop Forever must be the last entry in the chain.
The code is currently dimensioned to support a chain with roughly 600 entries and 20 loop counters.
#!/usr/bin/env python
import time
import pigpio
WAVES=5
GPIO=4
wid=[0]*WAVES
pi = pigpio.pi() # Connect to local Pi.
set_mode(pi, 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] = wave_create(pi, );
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 wave_tx_busy(pi, )
time.sleep(0.1);
for i in range(WAVES)
wave_delete(pi, wid[i])
stop(pi, )
PiGPIO.wave_clear
— MethodClears all waveforms and any data added by calls to the wave_add_*
functions.
wave_clear(pi, )
PiGPIO.wave_create
— MethodCreates a waveform from the data provided by the prior calls to the wave_add_*
functions.
Returns a wave id (>=0) if OK, otherwise PIEMPTYWAVEFORM, PITOOMANYCBS, PITOOMANYOOL, or PINOWAVEFORM_ID.
The data provided by the wave_add_*
functions is consumed by this function.
As many waveforms may be created as there is space available. The wave id is passed to wave_send_*
to specify the waveform to transmit.
Normal usage would be
Step 1. wave_clear
to clear all waveforms and added data.
Step 2. wave_add_*
calls to supply the waveform data.
Step 3. wave_create
to create the waveform and get a unique id
Repeat steps 2 and 3 as needed.
Step 4. wave_send_*
with the id of the waveform to transmit.
A waveform comprises one or more pulses.
A pulse specifies
- the GPIO to be switched on at the start of the pulse.
- the GPIO to be switched off at the start of the pulse.
- the delay in microseconds before the next pulse.
Any or all the fields can be zero. It doesn't make any sense to set all the fields to zero (the pulse will be ignored).
When a waveform is started each pulse is executed in order with the specified delay between the pulse and the next.
wid = wave_create(pi, )
PiGPIO.wave_delete
— MethodThis function deletes the waveform with id wave_id.
wave_id
: >=0 (as returned by a prior call towave_create
).
Wave ids are allocated in order, 0, 1, 2, etc.
wave_delete(pi, 6) # delete waveform with id 6
wave_delete(pi, 0) # delete waveform with id 0
PiGPIO.wave_get_cbs
— MethodReturns the length in DMA control blocks of the current waveform.
cbs = wave_get_cbs(pi, )
PiGPIO.wave_get_max_cbs
— MethodReturns the maximum possible size of a waveform in DMA control blocks.
cbs = wave_get_max_cbs(pi, )
PiGPIO.wave_get_max_micros
— MethodReturns the maximum possible size of a waveform in microseconds.
micros = wave_get_max_micros(pi, )
PiGPIO.wave_get_max_pulses
— MethodReturns the maximum possible size of a waveform in pulses.
pulses = wave_get_max_pulses(pi, )
PiGPIO.wave_get_micros
— MethodReturns the length in microseconds of the current waveform.
micros = wave_get_micros(pi, )
PiGPIO.wave_get_pulses
— MethodReturns the length in pulses of the current waveform.
pulses = wave_get_pulses(pi, )
PiGPIO.wave_send_once
— MethodTransmits the waveform with id wave_id. The waveform is sent once.
Any hardware PWM started by hardware_PWM
will be cancelled.
wave_id
: >=0 (as returned by a prior call towave_create
).
Returns the number of DMA control blocks used in the waveform.
cbs = wave_send_once(pi, wid)
PiGPIO.wave_send_repeat
— MethodPiGPIO.wave_send_repeat(self::Pi, wave_id)
Transmits the waveform with id wave_id
. The waveform repeats until wave_tx_stop
is called or another call to wave_send_*
is made.
Any hardware PWM started by hardware_PWM
will be cancelled.
wave_id
: >=0 (as returned by a prior call towave_create
).
Returns the number of DMA control blocks used in the waveform.
cbs = wave_send_repeat(pi, wid)
PiGPIO.wave_send_using_mode
— MethodTransmits the waveform with id wave_id using mode mode.
wave_id
: >0= (as returned by a prior call towave_create
).mode
:PiGPIO.WAVE_MODE_ONE_SHOT
,PiGPIO.WAVE_MODE_REPEAT
,PiGPIO.WAVE_MODE_ONE_SHOT_SYNC
, orPiGPIO.WAVE_MODE_REPEAT_SYNC
.
PiGPIO.WAVE_MODE_ONE_SHOT
: same as wave_send_once
.
PiGPIO.WAVE_MODE_REPEAT
same as wave_send_repeat
.
PiGPIO.WAVE_MODE_ONE_SHOT_SYNC
same as wave_send_once
but tries to sync with the previous waveform.
PiGPIO.WAVE_MODE_REPEAT_SYNC
same as wave_send_repeat
but tries to sync with the previous waveform.
Bad things may happen if you delete the previous waveform before it has been synced to the new waveform.
Any hardware PWM started by hardware_PWM
will be cancelled.
wave_id
: >=0 (as returned by a prior call towave_create
).
Returns the number of DMA control blocks used in the waveform.
cbs = wave_send_using_mode(pi, wid, PiGPIO.WAVE_MODE_REPEAT_SYNC)
PiGPIO.wave_tx_at
— MethodReturns the id of the waveform currently being transmitted.
Returns the waveform id or one of the following special values
PiGPIO.WAVE_NOT_FOUND
(9998) - transmitted wave not found. PiGPIO.NO_TX_WAVE
(9999) - no wave being transmitted.
wid = wave_tx_at(pi, )
PiGPIO.wave_tx_busy
— MethodReturns 1 if a waveform is currently being transmitted, otherwise 0.
wave_send_once(pi, 0) # send first waveform
while wave_tx_busy(pi, ): # wait for waveform to be sent
time.sleep(0.1)
wave_send_once(pi, 1) # send next waveform
PiGPIO.wave_tx_repeat
— MethodThis function is deprecated and has beeen removed.
Use wave_create
/wave_send_*
instead.
PiGPIO.wave_tx_start
— MethodThis function is deprecated and has been removed.
Use wave_create
/wave_send_*
instead.
PiGPIO.wave_tx_stop
— MethodStops the transmission of the current waveform.
This function is intended to stop a waveform started with wave_send_repeat
.
wave_send_repeat(pi, 3)
sleep(5)
wave_tx_stop(pi)
PiGPIO.write
— Methodwrite(self::Pi, gpio, level)
Sets the GPIO level for the pin gpio
(an integer between 0 and 53) where level is 0 or 1.
If PWM or servo pulses are active on the GPIO they are switched off.
set_mode(pi, 17, PiGPIO.OUTPUT)
write(pi, 17,0)
print(read(pi, 17))
# output 0
write(pi, 17,1)
print(read(pi, 17))
# output 1