PiGPIO API

PiGPIO.Callback_ADTType

An ADT class to hold callback information

  • gpio: Broadcom GPIO number.
  • edge: PiGPIO.EITHER_EDGE, PiGPIO.RISING_EDGE, or PiGPIO.FALLING_EDGE.
  • func: a user function taking three arguments (GPIO, level, tick).
source
PiGPIO.PiMethod
Pi(; 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
source
PiGPIO.PulseType

A 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.

source
PiGPIO._pigpio_commandFunction

Runs 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).
source
PiGPIO._pigpio_command_extFunction

Runs 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 extents
  • extents: additional data blocks
source
PiGPIO._u2iMethod

Converts 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.

source
PiGPIO.bb_i2c_closeMethod

This 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 to bb_i2c_open

Returns 0 if OK, otherwise PiGPIO.PI_BAD_USER_GPIO, or PiGPIO.PI_NOT_I2C_GPIO.

bb_i2c_close(pi, SDA)
source
PiGPIO.bb_i2c_openFunction

This 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-31
  • SCL: 0-31
  • baud: 50-500000

Returns 0 if OK, otherwise PiGPIO.PI_BAD_USER_GPIO, PiGPIO.PI_BAD_I2C_BAUD, or PiGPIO.PI_GPIO_IN_USE.

Note

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
source
PiGPIO.bb_i2c_zipMethod

This 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 to bb_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
source
PiGPIO.bb_serial_invertMethod

Invert serial logic.

  • user_gpio: 0-31 (opened in a prior call to bb_serial_read_open)
  • invert: 0-1 (1 invert, 0 normal)
status = bb_serial_invert(pi, 17, 1)
source
PiGPIO.bb_serial_readMethod

Returns data from the bit bang serial cyclic buffer.

  • user_gpio: 0-31 (opened in a prior call to bb_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)
source
PiGPIO.bb_serial_read_closeMethod

Closes a GPIO for bit bang reading of serial data.

  • user_gpio: 0-31 (opened in a prior call to bb_serial_read_open)
status = bb_serial_read_close(pi, 17)
source
PiGPIO.bb_serial_read_openFunction

Opens 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)
source
PiGPIO.callbackFunction
PiGPIO.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), or PiGPIO.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.
source
PiGPIO.clear_bank_1Method
PiGPIO.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)
source
PiGPIO.clear_bank_2Method
PiGPIO.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)
source
PiGPIO.custom_1Function
PiGPIO.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")
source
PiGPIO.custom_2Function
PiGPIO.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)
source
PiGPIO.error_textMethod
PiGPIO.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
source
PiGPIO.get_PWM_dutycycleMethod
PiGPIO.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
source
PiGPIO.get_PWM_frequencyMethod

Returns 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
source
PiGPIO.get_PWM_rangeMethod
PiGPIO.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
source
PiGPIO.get_PWM_real_rangeMethod
PiGPIO.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
source
PiGPIO.get_current_tickMethod
PiGPIO.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)
source
PiGPIO.get_hardware_revisionMethod
PiGPIO.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
source
PiGPIO.get_modeMethod
get_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
source
PiGPIO.get_servo_pulsewidthMethod

Returns 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
source
PiGPIO.hardware_PWMMethod
PiGPIO.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.

Note

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
source
PiGPIO.hardware_clockMethod
PiGPIO.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
source
PiGPIO.i2c_block_process_callMethod

Writes 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 to i2c_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])
source
PiGPIO.i2c_closeMethod

Closes the I2C device associated with handle.

  • handle: >=0 (as returned by a prior call to i2c_open).
i2c_close(pi, h)
source
PiGPIO.i2c_openFunction

Returns 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
source
PiGPIO.i2c_process_callMethod

Writes 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 to i2c_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)
source
PiGPIO.i2c_read_block_dataMethod

Reads 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 to i2c_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
source
PiGPIO.i2c_read_byteMethod

Reads a single byte from the device associated with handle.

  • handle: >=0 (as returned by a prior call to i2c_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
source
PiGPIO.i2c_read_byte_dataMethod

Reads a single byte from the specified register of the device associated with handle.

  • handle: >=0 (as returned by a prior call to i2c_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)
source
PiGPIO.i2c_read_deviceMethod

Returns count bytes read from the raw device associated with handle.

  • handle: >=0 (as returned by a prior call to i2c_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)
source
PiGPIO.i2c_read_i2c_block_dataMethod

Reads 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 to i2c_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
source
PiGPIO.i2c_read_word_dataMethod

Reads a single 16 bit word from the specified register of the device associated with handle.

  • handle: >=0 (as returned by a prior call to i2c_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)
source
PiGPIO.i2c_write_block_dataMethod

Writes up to 32 bytes to the specified register of the device associated with handle.

  • handle: >=0 (as returned by a prior call to i2c_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])
source
PiGPIO.i2c_write_byteMethod

Sends a single byte to the device associated with handle.

  • handle: >=0 (as returned by a prior call to i2c_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
source
PiGPIO.i2c_write_byte_dataMethod

Writes a single byte to the specified register of the device associated with handle.

  • handle: >=0 (as returned by a prior call to i2c_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)
source
PiGPIO.i2c_write_deviceMethod

Writes the data bytes to the raw device associated with handle.

  • handle: >=0 (as returned by a prior call to i2c_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])
source
PiGPIO.i2c_write_i2c_block_dataMethod

Writes 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 to i2c_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])
source
PiGPIO.i2c_write_quickMethod

Sends a single bit to the device associated with handle.

  • handle: >=0 (as returned by a prior call to i2c_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
source
PiGPIO.i2c_write_word_dataMethod

Writes a single 16 bit word to the specified register of the device associated with handle.

  • handle: >=0 (as returned by a prior call to i2c_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)
source
PiGPIO.i2c_zipMethod

This 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 to 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) = 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
source
PiGPIO.notify_beginMethod

Starts notifications on a handle.

  • handle: >=0 (as returned by a prior call to notify_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)
source
PiGPIO.notify_closeMethod

Stops notifications on a handle and releases the handle for reuse.

  • handle: >=0 (as returned by a prior call to notify_open)
h = notify_open(pi)
if h >= 0
    notify_begin(pi, h, 1234)
    # ...
    notify_close(pi, h)
    # ...
source
PiGPIO.notify_openMethod
PiGPIO.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)
source
PiGPIO.notify_pauseMethod

Pauses notifications on a handle.

  • handle: >=0 (as returned by a prior call to notify_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)
    # ...
source
PiGPIO.readMethod
read(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
source
PiGPIO.read_bank_1Method

Returns 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
source
PiGPIO.read_bank_2Method

Returns 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
source
PiGPIO.serial_closeMethod

Closes the serial device associated with handle.

  • handle: >=0 (as returned by a prior call to serial_open).
serial_close(pi, h1)
source
PiGPIO.serial_data_availableMethod

Returns the number of bytes available to be read from the device associated with handle.

  • handle: >=0 (as returned by a prior call to serial_open).
rdy = serial_data_available(pi, h1)

if rdy > 0
(b, d) = serial_read(pi, h1, rdy)
source
PiGPIO.serial_openFunction

Returns 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)
source
PiGPIO.serial_readMethod

Reads up to count bytes from the device associated with handle.

  • handle: >=0 (as returned by a prior call to serial_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
source
PiGPIO.serial_read_byteMethod

Returns a single byte from the device associated with handle.

  • handle: >=0 (as returned by a prior call to serial_open).
b = serial_read_byte(pi, h1)
source
PiGPIO.serial_writeMethod

Writes the data bytes to the device associated with handle.

  • handle: >=0 (as returned by a prior call to serial_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])
source
PiGPIO.serial_write_byteMethod

Writes a single byte to the device associated with handle.

  • handle: >=0 (as returned by a prior call to serial_open).
  • byte_val: 0-255, the value to write.
serial_write_byte(pi, h1, 23)

serial_write_byte(h1, ord(pi, 'Z'))
source
PiGPIO.set_PWM_dutycycleMethod
PiGPIO.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
source
PiGPIO.set_PWM_frequencyMethod

Sets 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
source
PiGPIO.set_PWM_rangeMethod
PiGPIO.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
source
PiGPIO.set_bank_1Method
PiGPIO.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)
source
PiGPIO.set_bank_2Method

Sets 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)
source
PiGPIO.set_modeMethod
set_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
source
PiGPIO.set_pull_up_downMethod
set_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)
source
PiGPIO.set_servo_pulsewidthMethod

Starts (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
source
PiGPIO.set_watchdogMethod

Sets 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
source
PiGPIO.spi_closeMethod

Closes the SPI device associated with handle.

  • handle: >=0 (as returned by a prior call to spi_open).
spi_close(pi, h)
source
PiGPIO.spi_openFunction

Returns 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.

Warning

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)
source
PiGPIO.spi_readMethod

Reads count bytes from the SPI device associated with handle.

  • handle: >=0 (as returned by a prior call to spi_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
source
PiGPIO.spi_writeMethod

Writes the data bytes to the SPI device associated with handle.

  • handle: >=0 (as returned by a prior call to spi_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
source
PiGPIO.spi_xferMethod

Writes 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 to spi_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")
source
PiGPIO.stopMethod
PiGPIO.stop(self::Pi)

Release pigpio resources.

PiGPIO.stop(pi)
source
PiGPIO.tallyMethod

Provides 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.

source
PiGPIO.tickDiffMethod
PiGPIO.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
source
PiGPIO.wait_for_edgeFunction

Wait for an edge event on a GPIO.

  • user_gpio: 0-31.
  • edge: PiGPIO.EITHER_EDGE, PiGPIO.RISING_EDGE (default), or PiGPIO.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")
source
PiGPIO.wave_add_genericMethod

Adds 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
source
PiGPIO.wave_add_newMethod

Starts 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, )
source
PiGPIO.wave_add_serialFunction

Adds 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)
source
PiGPIO.wave_chainMethod

This function transmits a chain of waveforms.

Note

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_ids 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, )
source
PiGPIO.wave_clearMethod

Clears all waveforms and any data added by calls to the wave_add_* functions.

wave_clear(pi, )
source
PiGPIO.wave_createMethod

Creates 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

  1. the GPIO to be switched on at the start of the pulse.
  2. the GPIO to be switched off at the start of the pulse.
  3. 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, )
source
PiGPIO.wave_deleteMethod

This function deletes the waveform with id wave_id.

  • wave_id: >=0 (as returned by a prior call to wave_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
source
PiGPIO.wave_get_cbsMethod

Returns the length in DMA control blocks of the current waveform.

cbs = wave_get_cbs(pi, )
source
PiGPIO.wave_send_onceMethod

Transmits the waveform with id wave_id. The waveform is sent once.

Note

Any hardware PWM started by hardware_PWM will be cancelled.

  • wave_id: >=0 (as returned by a prior call to wave_create).

Returns the number of DMA control blocks used in the waveform.

cbs = wave_send_once(pi, wid)
source
PiGPIO.wave_send_repeatMethod
PiGPIO.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.

Note

Any hardware PWM started by hardware_PWM will be cancelled.

  • wave_id: >=0 (as returned by a prior call to wave_create).

Returns the number of DMA control blocks used in the waveform.

cbs = wave_send_repeat(pi, wid)
source
PiGPIO.wave_send_using_modeMethod

Transmits the waveform with id wave_id using mode mode.

  • wave_id: >0= (as returned by a prior call to wave_create).
  • mode: PiGPIO.WAVE_MODE_ONE_SHOT, PiGPIO.WAVE_MODE_REPEAT, PiGPIO.WAVE_MODE_ONE_SHOT_SYNC, or PiGPIO.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.

Warning

Bad things may happen if you delete the previous waveform before it has been synced to the new waveform.

Note

Any hardware PWM started by hardware_PWM will be cancelled.

  • wave_id: >=0 (as returned by a prior call to wave_create).

Returns the number of DMA control blocks used in the waveform.

cbs = wave_send_using_mode(pi, wid, PiGPIO.WAVE_MODE_REPEAT_SYNC)
source
PiGPIO.wave_tx_atMethod

Returns 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, )
source
PiGPIO.wave_tx_busyMethod

Returns 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
source
PiGPIO.wave_tx_stopMethod

Stops 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)
source
PiGPIO.writeMethod
write(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
source