The hardware interfaces provide an interface between the components (sensors and actuators) of the 2WD robot and the main processing unit, the Raspberry Pi 4 B.

GPIO

Currently, three GPIO pins are used to connect the ultrasonic ranger and two speed sensors.

The ultrasonic ranger uses just a single GPIO pin for communicating its measured distances. Therefore, we can use one of the GPIO pins such as physical pin 11.

The LM393 speed sensors also use a single digital GPIO pin each. These pins will be setup using software interrupts with the RPi.GPIO library.

Prepare I2C Connection

The I2C connections are used for multiple components such as the motor driver and the oled display. Using these ports on the Raspberry Pi 4 B, requires that we enable the I2C interface.

To do so, we will use the tool i2cdetect which requires that we install a tool on Ubuntu called i2c-tools:

fjp@ubuntu:~/git/2wd-robot$ i2cdetect

Command 'i2cdetect' not found, but can be installed with:

sudo apt install i2c-tools

fjp@ubuntu:~/git/2wd-robot$ sudo apt install i2c-tools

To test if the i2c ports are working we use the following commands:

$ i2cdetect -y 0
Error: Could not open file '/dev/i2c-0' or '/dev/i2c/0': No such file or directory
$ i2cdetect -y 1
Error: Could not open file '/dev/i2c-1' or '/dev/i2c/1': No such file or directory

The ports are not setup correctly yet, which is why we need to enable the following two lines in the /boot/firmware/config.txt file:

dtparam=i2c0=on
dtparam=i2c1=on
I2C Pinout on Raspberry Pi 4 B.

On Raspian Buster, the official Raspberry OS, we could use the raspi-config tool:

fjp@ubuntu:~/git/2wd-robot$ sudo raspi-config
Raspberry Pi 4 Model B Rev 1.1



┌──────────────────┤ Raspberry Pi Software Configuration Tool (raspi-config) ├───────────────────┐
│                                                                                                │
│      1 Change User Password Change password for the current user                               │
│      2 Network Options      Configure network settings                                         │
│      3 Boot Options         Configure options for start-up                                     │
│      4 Localisation Options Set up language and regional settings to match your location       │
5 Interfacing Options  Configure connections to peripherals                        
│      6 Overclock            Configure overclocking for your Pi                                 │
│      7 Advanced Options     Configure advanced settings                                        │
│      8 Update               Update this tool to the latest version                             │
│      9 About raspi-config   Information about this configuration tool                          │
│                                                                                                │
│                                                                                                │
│                                                                                                │
│                           <Select>                           <Finish>                          │
│                                                                                                │
└────────────────────────────────────────────────────────────────────────────────────────────────┘

Select the i2c option:




┌──────────────────┤ Raspberry Pi Software Configuration Tool (raspi-config) ├───────────────────┐
│                                                                                                │
│        P1 Camera      Enable/Disable connection to the Raspberry Pi Camera                     │
│        P2 SSH         Enable/Disable remote command line access to your Pi using SSH           │
│        P3 VNC         Enable/Disable graphical remote access to your Pi using RealVNC          │
│        P4 SPI         Enable/Disable automatic loading of SPI kernel module                    │
P5 I2C         Enable/Disable automatic loading of I2C kernel module            
│        P6 Serial      Enable/Disable shell and kernel messages on the serial connection        │
│        P7 1-Wire      Enable/Disable one-wire interface                                        │
│        P8 Remote GPIO Enable/Disable remote access to GPIO pins                                │
│                                                                                                │
│                                                                                                │
│                                                                                                │
│                                                                                                │
│                           <Select>                           <Back>                            │
│                                                                                                │
└────────────────────────────────────────────────────────────────────────────────────────────────┘


And enable the interface:



                   ┌──────────────────────────────────────────────────────────┐
                   │                                                          │ 
                   │ Would you like the ARM I2C interface to be enabled?      │ 
                   │                                                          │ 
                   │                                                          │ 
                   │                                                          │ 
                   │                                                          │ 
                   │                                                          │ 
                   │                                                          │ 
                   │                                                          │ 
                   │                                                          │ 
                   │                                                          │ 
                   │                                                          │ 
                   │                                                          │ 
                   │                                                          │ 
                   │                                                          │ 
                   │                                                          │ 
                   <Yes>                  <No>                │ 
                   │                                                          │ 
                   └──────────────────────────────────────────────────────────┘ 
                                                                                

Confirm the activation and restart the RPi:



                   ┌──────────────────────────────────────────────────────────┐
                   │                                                          │ 
                   │ The ARM I2C interface is enabled                         │ 
                   │                                                          │ 
                   │                                                          │ 
                   │                                                          │ 
                   │                                                          │ 
                   │                                                          │ 
                   │                                                          │ 
                   │                                                          │ 
                   │                                                          │ 
                   │                                                          │ 
                   │                                                          │ 
                   │                                                          │ 
                   │                                                          │ 
                   │                                                          │ 
                   │                                                          │ 
                   <Ok> 
                   │                                                          │ 
                   └──────────────────────────────────────────────────────────┘ 
                                                                                

Leave a comment