lg archive
lg lgpio C lgpio Python Daemon rgpio C rgpio Python rgs Download Examples FAQ Site Map

Examples

The following examples show various ways lg may be used to communicate with sensors via the GPIO.

Although many are complete programs they are intended to be a starting point in producing your own code, not an end point.

Index

C lgpio

Python lgpio

C rgpio

Python rgpio

rgs shell

C lgpio

Examples of C lgpio programs.

C lgpio programs may be run on any Linux computer. The programs allow control of the local GPIO.

If your program is called foobar.c then build with

gcc -Wall -o foobar foobar.c -llgpio


28BYJ_48
2020-11-21
Simple code to spin a 28BYJ_48 stepper clockwise then counterclockwise.

./28BYJ_48 [chip] gpio1 gpio2 gpio3 gpio4

GPIO Toggle
2020-11-18
A crude benchmark of how many toggles a second can be completed.

gpiochip Info
2020-11-18
Prints gpiochip information and line information for each GPIO.

DHT11/22/XX
2020-11-18
Reads the DHT series of temperature and humidity sensors. The sensor type is autodetected.

ADS1x15 ADC (.h)
2021-01-17
ADS1x15 header file.

ADS1x15 ADC
2021-01-17
ADS1015 (12-bit) and ADS1115 (16-bit) ADC functions. The ALERT/RDY
pin is fully supported.

MCP3008 ADC (.h)
2021-01-09
MCP3008 header file.

MCP3008 ADC
2021-01-09
MCP3008 ADC functions.

MCP4131 Digital Pot (.h)
2021-01-09
MCP4131 header file.

MCP4131 Digital Pot
2021-01-09
MCP4131 digital pot functions.

Monitor GPIO
2020-11-20
Monitors one or more GPIO as specified on the command line.

./monitor [chip:]gpio ...

chip specifies a gpiochip number. gpio is a GPIO in the previous gpiochip (gpiochip0 if there is no previous gpiochip).

e.g.

./monitor 23 24 25 # chip0: 23,24,25
./monitor 0:23 1:0 5 # chip0: 23, chip1: 0,5

NRF24 Radio Transceiver
2020-12-30
Script to transmit and receive messages using the nRF24L01 radio transceiver.

Rotary Encoder
2020-11-20
Code to monitor a rotary encoder and show the detent changes.

./rotary_encoder [chip] gpioA gpioB

E.g.

./rotary_encoder 20 21 # gpiochip 0, gpioA 20, gpioB 21

./rotary_encoder 2 7 5 # gpiochip 2, gpioA 7, gpioB 5

Sonar Ranger
2020-11-20
Code to find the distance between a sonar ranger and the nearest object. The code assumes the sonar ranger is the type with separate trigger and echo lines.

./sonar_ranger [chip] trigger echo

E.g.

./sonar_ranger 20 21 # gpiochip 0, trigger 20, echo 21

./sonar_ranger 2 7 5 # gpiochip 2, trigger 7, echo 5

Tx Pulse
2020-11-18
Demonstrates usage of the tx pulse function.

Tx Wave
2020-11-18
Demonstrates usage of the tx wave function.


Python lgpio

Examples of Python lgpio module scripts.

lgpio Python scripts may be run on any Linux computer with Python installed. The scripts allow control of the local GPIO.


28BYJ_48
2020-11-21
Simple code to spin a 28BYJ_48 stepper clockwise then counterclockwise.

./28BYJ_48.py [chip] gpio1 gpio2 gpio3 gpio4

GPIO Toggle
2020-11-18
A crude benchmark of how many toggles a second can be completed.

gpiochip Info
2020-11-18
Prints gpiochip information and line information for each GPIO.

DHT11/22/XX
2020-12-01
Reads the DHT series of temperature and humidity sensors. The sensor type is autodetected.

Errors
2020-11-18
Checks that the correct errors are reported when erroneous parameters are used. The checks are limited in scope to those that can be detected without having any hardware connected.

ADS1x15 ADC
2021-01-17
ADS1015 (12-bit) and ADS1115 (16-bit) ADC functions. The ALERT/RDY
pin is fully supported.

MCP3008 ADC
2021-01-09
MCP3008 ADC functions.

MCP4131 Digital Pot
2021-01-09
MCP4131 digital pot functions.

Monitor GPIO
2021-01-11
Monitors one or more GPIO as specified on the command line.

./monitor.py [chip:]gpio ...

chip specifies a gpiochip number. gpio is a GPIO in the previous gpiochip (gpiochip0 if there is no previous gpiochip).

e.g.

./monitor.py 23 24 25 # chip0: 23,24,25
./monitor.py 0:23 1:0 5 # chip0: 23, chip1: 0,5

Motor Sync
2020-11-30
A class to send synchronised outputs to output devices. Unipolar steppers and servos are specifically supported. PWM is supported. An adhoc method allows for setting the levels of one or more GPIO at set times.

The following steps are required.

1. sync = motor_sync.sync(sbc)
2. call one or more setup_() functions to define the devices and the GPIO to use.
3. call setup_complete() when all devices have been allocated.
4. add waypoints for each device to be moved in the next period.
5. call batch() to start transmission.
6. wait until batching() is False (all movements calculated).
7. If more movements are desired repeat from step 4.
8. wait till busy() is False (all movements completed).
9. call finish() to release resources.

NRF24 Radio Transceiver
2020-12-22
Script to transmit and receive messages using the nRF24L01 radio transceiver.

Rotary Encoder
2020-11-18
Code to monitor a rotary encoder and show the detent changes.

./rotary_encoder.py [chip] gpioA gpioB

E.g.

./rotary_encoder.py 20 21 # gpiochip 0, gpioA 20, gpioB 21

./rotary_encoder.py 2 7 5 # gpiochip 2, gpioA 7, gpioB 5

Sonar Ranger
2020-11-20
Code to find the distance between a sonar ranger and the nearest object. The code assumes the sonar ranger is the type with separate trigger and echo lines.

./sonar_ranger.py [chip] trigger echo

E.g.

./sonar_ranger.py 20 21 # gpiochip 0, trigger 20, echo 21

./sonar_ranger.py 2 7 5 # gpiochip 2, trigger 7, echo 5

Testbed
2020-12-31
Exercises SPI, I2C, and serial links with miscellaneous hardware.

Tx Pulse
2020-11-18
Demonstrates usage of the tx pulse function.

Tx Wave
2020-11-18
Demonstrates usage of the tx wave function.


C rgpio

Examples of C rgpio programs.

C rgpio programs may be run on any Linux computer. The programs allow control of the GPIO on one or more networked Linux SBCs.

Each SBC needs the rgpiod daemon to be running. The rgpiod daemon may be started with the command rgpiod &.

If your program is called foobar.c then build with

gcc -Wall -o foobar foobar.c -lrgpio


28BYJ_48
2020-11-21
Simple code to spin a 28BYJ_48 stepper clockwise then counterclockwise.

./28BYJ_48 [chip] gpio1 gpio2 gpio3 gpio4

GPIO Toggle
2020-11-18
A crude benchmark of how many toggles a second can be completed.

gpiochip Info
2020-11-18
Prints gpiochip information and line information for each GPIO.

DHT11/22/XX
2020-11-18
Reads the DHT series of temperature and humidity sensors. The sensor type is autodetected.

DS18B20
2020-11-18
Reads any DS18B20 temperature sensors connected to the 1-wire bus. It demonstrates the remote file read abilities of the daemon.

Errors
2020-11-18
Checks that the correct errors are reported when erroneous parameters are used. The checks are limited in scope to those that can be detected without having any hardware connected.

Files
2020-11-18
Demonstrates usage of the remote file handling features of the daemon.

ADS1x15 ADC (.h)
2021-01-17
ADS1x15 header file.

ADS1x15 ADC
2021-01-17
ADS1015 (12-bit) and ADS1115 (16-bit) ADC functions. The ALERT/RDY
pin is fully supported.

MCP3008 ADC (.h)
2021-01-09
MCP3008 header file.

MCP3008 ADC
2021-01-09
MCP3008 ADC functions.

MCP4131 Digital Pot (.h)
2021-01-09
MCP4131 header file.

MCP4131 Digital Pot
2021-01-09
MCP4131 digital pot functions.

Monitor GPIO
2020-11-18
Monitors one or more GPIO as specified on the command line.

./monitor [chip:]gpio ...

chip specifies a gpiochip number. gpio is a GPIO in the previous gpiochip (gpiochip0 if there is no previous gpiochip).

e.g.

./monitor 23 24 25 # chip0: 23,24,25
./monitor 0:23 1:0 5 # chip0: 23, chip1: 0,5

NRF24 Radio Transceiver
2020-12-30
Script to transmit and receive messages using the nRF24L01 radio transceiver.

Rotary Encoder
2020-11-18
Code to monitor a rotary encoder and show the detent changes.

./rotary_encoder [chip] gpioA gpioB

E.g.

./rotary_encoder 20 21 # gpiochip 0, gpioA 20, gpioB 21

./rotary_encoder 2 7 5 # gpiochip 2, gpioA 7, gpioB 5

Sonar Ranger
2020-11-20
Code to find the distance between a sonar ranger and the nearest object. The code assumes the sonar ranger is the type with separate trigger and echo lines.

./sonar_ranger [chip] trigger echo

E.g.

./sonar_ranger 20 21 # gpiochip 0, trigger 20, echo 21

./sonar_ranger 2 7 5 # gpiochip 2, trigger 7, echo 5

Tx Pulse
2020-11-18
Demonstrates usage of the tx pulse function.

Tx Wave
2020-11-18
Demonstrates usage of the tx wave function.


Python rgpio

Examples of Python rgpio module scripts.

The rgpio Python module may be run on any computer with Python installed. The scripts allow control of the GPIO on one or more networked Linux SBCs.

The computer need not be Linux, it may run Windows, Mac, etc, anything as long as it supports Python.

Each SBC needs the rgpiod daemon to be running. The rgpiod daemon may be started with the command rgpiod &.


28BYJ_48
2020-11-21
Simple code to spin a 28BYJ_48 stepper clockwise then counterclockwise.

./28BYJ_48.py [chip] gpio1 gpio2 gpio3 gpio4

GPIO Toggle
2020-11-18
A crude benchmark of how many toggles a second can be completed.

gpiochip Info
2020-11-18
Prints gpiochip information and line information for each GPIO.

DHT11/22/XX
2020-11-18
Reads the DHT series of temperature and humidity sensors. The sensor type is autodetected.

DS18B20
2020-11-18
Reads any DS18B20 temperature sensors connected to the 1-wire bus. It demonstrates the remote file read abilities of the daemon.

Errors
2020-11-18
Checks that the correct errors are reported when erroneous parameters are used. The checks are limited in scope to those that can be detected without having any hardware connected.

Files
2020-11-18
Demonstrates usage of the remote file handling features of the daemon.

ADS1x15 ADC
2021-01-17
ADS1015 (12-bit) and ADS1115 (16-bit) ADC functions. The ALERT/RDY
pin is fully supported.

MCP3008 ADC
2021-01-09
MCP3008 ADC functions.

MCP4131 Digital Pot
2021-01-09
MCP4131 digital pot functions.

Monitor GPIO
2021-01-11
Monitors one or more GPIO as specified on the command line.

./monitor.py [chip:]gpio ...

chip specifies a gpiochip number. gpio is a GPIO in the previous gpiochip (gpiochip0 if there is no previous gpiochip).

e.g.

./monitor.py 23 24 25 # chip0: 23,24,25
./monitor.py 0:23 1:0 5 # chip0: 23, chip1: 0,5

Motor Sync
2020-11-30
A class to send synchronised outputs to output devices. Unipolar steppers and servos are specifically supported. PWM is supported. An adhoc method allows for setting the levels of one or more GPIO at set times.

The following steps are required.

1. sync = motor_sync.sync(sbc)
2. call one or more setup_() functions to define the devices and the GPIO to use.
3. call setup_complete() when all devices have been allocated.
4. add waypoints for each device to be moved in the next period.
5. call batch() to start transmission.
6. wait until batching() is False (all movements calculated).
7. If more movements are desired repeat from step 4.
8. wait till busy() is False (all movements completed).
9. call finish() to release resources.

NRF24 Radio Transceiver
2020-12-22
Script to transmit and receive messages using the nRF24L01 radio transceiver.

Rotary Encoder
2020-11-18
Code to monitor a rotary encoder and show the detent changes.

./rotary_encoder.py [chip] gpioA gpioB

E.g.

./rotary_encoder.py 20 21 # gpiochip 0, gpioA 20, gpioB 21

./rotary_encoder.py 2 7 5 # gpiochip 2, gpioA 7, gpioB 5

Sonar Ranger
2020-11-20
Code to find the distance between a sonar ranger and the nearest object. The code assumes the sonar ranger is the type with separate trigger and echo lines.

./sonar_ranger.py [chip] trigger echo

E.g.

./sonar_ranger.py 20 21 # gpiochip 0, trigger 20, echo 21

./sonar_ranger.py 2 7 5 # gpiochip 2, trigger 7, echo 5

Testbed
2020-12-31
Exercises SPI, I2C, and serial links with miscellaneous hardware.

Tx Pulse
2020-11-18
Demonstrates usage of the tx pulse function.

Tx Wave
2020-11-18
Demonstrates usage of the tx wave function.



rgs command line

gpiochip Info
2021-01-03
Prints gpiochip information and line information for each GPIO.



Index

28BYJ_48 lgpio py_lgpio rgpio py_rgpio
ADS1x15 ADC lgpio py_lgpio rgpio py_rgpio
ADS1x15 ADC (.h) lgpio rgpio
DHT11/22/XX lgpio py_lgpio rgpio py_rgpio
DS18B20 rgpio py_rgpio
Errors py_lgpio rgpio py_rgpio
Files rgpio py_rgpio
GPIO Toggle lgpio py_lgpio rgpio py_rgpio
gpiochip Info lgpio py_lgpio rgpio py_rgpio rgs
MCP3008 ADC lgpio py_lgpio rgpio py_rgpio
MCP3008 ADC (.h) lgpio rgpio
MCP4131 Digital Pot lgpio py_lgpio rgpio py_rgpio
MCP4131 Digital Pot (.h) lgpio rgpio
Monitor GPIO lgpio py_lgpio rgpio py_rgpio
Motor Sync py_lgpio py_rgpio
NRF24 Radio Transceiver lgpio py_lgpio rgpio py_rgpio
Rotary Encoder lgpio py_lgpio rgpio py_rgpio
Sonar Ranger lgpio py_lgpio rgpio py_rgpio
Testbed py_lgpio py_rgpio
Tx Pulse lgpio py_lgpio rgpio py_rgpio
Tx Wave lgpio py_lgpio rgpio py_rgpio
[lg] [lgpio C] [lgpio Python] [Daemon] [rgpio C] [rgpio Python] [rgs] [Download] [Examples] [FAQ] [Site Map]
© 2020-2021
(2021-01-16 21:12)