Skip to content
ImagineWorld

Exploring Robot Building Blocks – Code and Electronics

node_idnode-book_learn-robotics-programming_chapter-02-exploring-robot-building-blocks-code-and-electronics
node_typechapter-node
schema_profileworld.chapter_node.v1
statusactive
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
chapter_num2
chapter_titleExploring Robot Building Blocks – Code and Electronics
governed_byCORPUS
constitutional_citationCONSTITUTION
local graph

Chapter 2 — Exploring Robot Building Blocks – Code and Electronics

Section titled “Chapter 2 — Exploring Robot Building Blocks – Code and Electronics”

Exploring Robot Building Blocks – Code and Electronics

Section titled “Exploring Robot Building Blocks – Code and Electronics”

In this chapter, we’ll plan our robot. We’ll think about what you want the robot to do, and what parts you’ll need to achieve this. We’ll sketch block diagrams of the software and hardware for your robot to generate ideas and use them as a guide to the connected code and parts. We will then look at physical robot design.

We will make choices about the main controller – finding out about the Raspberry Pi and its features. We will then specify a main breakout with the motor driver, and how we will power the robot. By the end of this chapter, you will be ready to buy the parts needed to build your robot.

In this chapter, we will be covering the following topics:

  • Planning components and code structure
  • Planning the physical robot
  • Choosing a robot chassis kit
  • Exploring controllers and I/O
  • Exploring Raspberry Pi’s capabilities
  • Choosing a main breakout board
  • Powering the robot
  • Test fitting the robot

For this chapter, you will require the following:

  • Drawing materials, such as a pen and paper
  • Computer with internet connection
  • Diagram software such as Inkscape (free at https://inkscape.org)

Before you build a robot, consider what you’d like it to do. What code and software would you run? In this section, we’ll make a block diagram of the software we want, and what hardware it may need to connect to. This will help drive other plans.

We will also consider how the code is structured. Code is easier to reason about when taken as logical blocks instead of one large lump. Arranging code in layers and blocks will help you navigate your way around as it becomes more complicated.

Let’s start with a specific use case, tracking an object. From this use case, we are going to want to handle a camera and drive wheels, which would lead to this logical hardware plan:

Figure 2.1 – Block diagram of robot parts

Figure 2.1 – Block diagram of robot parts

Figure 2.1 – Block diagram of robot parts

We’d need the hardware in Figure 2.1 (a sensor and two outputs – a left and right motor) for the capability described. This helps us to think about what our code would need to handle. Each component (camera and motors) may need some code to deal with it, and then you need some code for the behavior of combined modules.

Each hardware module shown could potentially come in many flavors, with different interfaces needed to read similar data or make similar outputs. As an example, different motor drivers need different code to operate their motors. When we write behavior for a robot, we do not want to rewrite it if we change the motor driver. Mixing motor driver code with behavior code also makes it harder to reason about.

For dealing with hardware variation, I recommend creating an interface layer, an abstraction between the real motor driver code and a standard interface, to make swapping components possible. We will see this in practice in Chapter 6*,*Drive and Turn – Moving Motors with MQTT and Python.

This is similar for each sensor or module. They will have some code to manage how they get signals and turn them into usable data. All these devices may have setup and teardown code that needs to run when starting or stopping a behavior that connects to them. The camera is a sophisticated example of this, requiring processing to get the data we can use to perform a task.

We can represent our software choices with a diagram:

Figure 2.2 – A software block diagram in pen on an envelope (top) and the same diagram using software (bottom)

Figure 2.2 – A software block diagram in pen on an envelope (top) and the same diagram using software (bottom)

Figure 2.2 – A software block diagram in pen on an envelope (top) and the same diagram using software (bottom)

Just like the hardware, a diagram can represent the software. I encourage trying both methods shown in *Figure 2.*2 – first, as a pen sketch on paper and then using a computer diagramming tool. Quick sketches are important to get ideas out without worrying too much about arranging them. It doesn’t need to be tidy, but it’s quickly redrawn and can even be done on the back of receipt paper if an idea comes to you while out dining. You can use pen and paper to write ideas down where and when they occur.

Scan/take photos of hand-drawn sketches with a smartphone before they fade. Putting them into software such as Obsidian as images/PDFs with useful tags lets you look them up quickly later.

You can then upgrade your hand-drawn sketch with a software tool such as Draw.io (diagrams.net), Inkscape, or Word. Try not to be distracted by the quirks and styling of a tool.

The diagram describes using a camera to drive toward an object. The blocks are as follows:

  • Motors: This block will control the motor drive, likely using code written by the motor driver manufacturer or setting Input/Output (I/O) pins connected to the controller. It receives motor commands from other blocks.
  • Camera: This block will configure camera setup, including resolution and white balancing as well as capturing and streaming images from the camera.
  • Object position: This block will receive the stream of images from the camera block, processing them to extract object position data.
  • Monitoring: Users debug and monitor the robot with this block. It will receive monitoring and logging data.
  • Face object and drive: This block represents a behavior; a behavior is code that performs a task and tends to connect the other components together. In this case, it will receive object position data from the O bject position block, then think and act by sending control signals to the M otors block. It will also send monitoring data to the M onitoring block.

When we break code down into blocks like this, we can approach a single block at a time to understand, test, and tune its code, and then focus on another one. We can make a collection of such blocks, then mix and match them, reusing the motors block with multiple behaviors, or the behavior on other robots with a modified motors block to fit that robot. We don’t need to write the same behavior again on each new robot.

We can implement different software blocks and their interactions in different ways. We can consider whether we will use functions, classes, or services for these blocks.

We’ve only considered tracking an object. However, we will explore many behaviors in this book – remote driving, speech control, localization, obstacle avoidance, looking at faces, driving known turns or distances, and line following. In the following sections, we’ll see what sensors we’ll need to support this much functionality, but first, let’s look at where this code would need to run and what it would need to be capable of.

We can make an outline of the physical robot to account for what we would like the robot to do. We can use a capability outline to make decisions about what parts we will use and how we will connect them. We should also consider any safety guidelines and precautions required for the robot. We will add components to the robot as we go through chapters; keeping an overall map in our minds as we go helps us to see where we are.

It is quite exciting to start to picture all the things a robot will do. A list gives us an idea without getting lost in the details. What do we want our robot to do?

  • Drive on the floor
  • Indicate its status
  • Look at faces or detect objects
  • Follow lines
  • Avoid walls
  • Navigate spaces
  • Respond to voice commands
  • Be controllable from a web app or smartphone
  • Work safely

What will our robot need?

  • Wheels and motors
  • A motor driver
  • Some multicolored LEDs
  • Distance sensors to avoid walls and navigate
  • Wheel encoders to know how far it has moved
  • A camera on a pan and tilt mechanism
  • A microphone and speaker
  • Smartphone/web app code
  • A powerful main controller to orchestrate them
  • A power supply

Phew! That is a lot of functionality. We can draw hardware blocks to try and capture these and how they could be connected:

Figure 2.3 – Block diagram of the robot we will build

Figure 2.3 – Block diagram of the robot we will build

Figure 2.3 – Block diagram of the robot we will build

*Figure 2.*3 shows the robot we’ll build as a block diagram. This was made in Inkscape, but a pen sketch of a block diagram is also an excellent start to robot planning. Most of my robots start off that way. This uses the sensors we saw in the previous chapter. The annotation here is not any formal notation, but a way of visualizing all the parts that we’ll connect. We will see the controllers and how to power them later in this chapter.

We have a main controller, connected to a motor driver. We will dive deeper into this component later in this chapter. The motor driver powers the motors, which will turn the wheel assemblies.

Those assemblies have encoders to sense wheel motions and feed that back to the main controller. The motor driver also controls pan and tilt servos in a camera assembly, with the camera sending data back to the main controller.

There are distance sensors and an Inertial Measurement Unit (IMU) sending sensor data back to the main controller. This robot also has a few LED lights for user feedback. However, most user interaction will come via the connected smartphone, and some via the speaker and microphone.

A Light - Emitting Diode (LED) is an electronic light. It is a diode, which only allows current through one way. However, the LED’s most useful properties for us are they are small, can be controlled digitally, and use low power.

Although this looks like a daunting number of robot parts, we will focus on an area of functionality in each chapter and build, test, and debug it before moving to other areas.

Along with this, I sketch roughly where I would physically place sensors and parts with one another:

Figure 2.4 – An overview of how the robot could be physically laid out

Figure 2.4 – An overview of how the robot could be physically laid out

Figure 2.4 – An overview of how the robot could be physically laid out

Before we choose a chassis or get into the specifics, we can make a sketch like the one in *Figure 2.*4. This is a rough, not-to-scale idea of where we want the parts to end up. Let’s look at the elements of the diagram:

  • The largest part is the chassis. We’ll build all the other parts around it.
  • This robot can have a single distance sensor, with multiple measurements at point A. It’s at the front with a clear field of view.
  • We’ll put a pan and tilt mechanism on top of the chassis at point B, with the camera attached.
  • We can put the two motors (C) on a lower deck or under the chassis, along with the encoder and wheel assemblies.
  • We can add an IMU (D) a little over the main controller, giving it some distance from the motors.
  • We’ll put the main controller (E) on the top of the chassis, so we can get to the wiring. This will also likely have the motor driver board stacked on it.
  • Heavy items, specifically batteries (F), should be kept low (below the center of gravity) to avoid the robot tipping over. Batteries need to be changed, so think about accessing them.
  • I’ve not drawn the smartphone, and we’ll use a wireless headset for speakers/microphone.

This rough plan need not be very detailed and is not the test fit. Real dimensions, design compromises, and hitches will mean that this will change. This is just a starting point.

As we work through the book, we will look at the details in these diagrams. We’ll start with the chassis kit.

The chassis is a fundamental decision when making a robot. Although you could make one using 3D printing or toy hacking, a good place to start is with a chassis kit. These kits contain sets of parts to start your robot build. A chassis can be changed, but it would mean rebuilding the robot.

The internet has plenty of chassis kits around – too many. So how do you choose one?

Getting the size of a robot right matters. Make a robot too small and you will need miniaturization skills for electronics, too large and you will need far more serious power handling and safety considerations. These are both things to avoid for a beginner.

Let’s compare some robot sizes:

Figure 2.5 – Robot chassis sizes compared

Figure 2.5 – Robot chassis sizes compared

Figure 2.5 – Robot chassis sizes compared

The chassis photos in Figure 2.5 are at different scales, with the long dimensions shown. Why would some chassis sizes make more sense than others?

  • Chassis A is small, only 100 mm, barely larger than the Raspberry Pi. Being so small makes it hard to build your robot. Squeezing the controller, power, and all the sensors into this small space would need skill and experience beyond the scope of a first robot build. You can stack these vertically, but they are suited to smaller controllers only.
  • Chassis B is a large robot for a beginner. This robot is 400 mm by 300 mm wide, with an arm reach of 600 mm. The big robot comes with big robot problems. It needs big batteries, big motors, a powerful motor controller, longer cables, and cable management. These add to the expense and may cause issues around power handling, weight, and rigidity for a new builder. Armbot is one of my most expensive robots, excluding the cost of the arm!
  • Chassis C is 200 mm and will comfortably fit a Raspberry Pi, batteries, and sensors. It is between 100–150 mm wide. Chassis C has enough space and is relatively easy to build.

Let’s look at the wheel count next.

Some chassis have elaborate movement methods – legs, tank tracks, mecanum wheels, and tri-star wheels, to name a few.

Figure 2.6 – Complex movement types

Figure 2.6 – Complex movement types

Figure 2.6 – Complex movement types

Figure 2.6 shows some of the more complex movement types. Tri-star wheels are compound wheels designed to climb by rotating the whole assembly over an obstacle. Tank tracks look great and can handle complex terrain, but they come with many mechanical issues. Legs can mean powering and configuring many servo motors or complicated gear linkages. Mecanum wheels allow for interesting movement but require more programming and motors to work.

While these are fun and I encourage experimenting with them, this is not the place to start. I recommend thoroughly sensible and basic wheels on your first robot.

There are kits with four-wheel drive and six-wheel drive:

Figure 2.7 – Four-wheel and six-wheel drive robots

Figure 2.7 – Four-wheel and six-wheel drive robots

Figure 2.7 – Four-wheel and six-wheel drive robots

While four-wheel and six-wheel drive robots like the ones in *Figure 2.*7 have more power, that means larger motor controllers and they may also chew through batteries or complex gearchains. You are also increasing the likelihood of overloading something, and the additional motors can mean trickier wiring.

A two-wheel drive is the simplest:

Figure 2.8 – Two wheels with a castor

Figure 2.8 – Two wheels with a castor

Figure 2.8 – Two wheels with a castor

Two wheels won’t have the pulling power of four- or six-wheel drives, but they are simple and work. They usually require a third wheel for balance. The third wheel can be a castor wheel, a rollerball, or just a skid. Two wheels are also the easiest to steer, with pulling around the skid resulting in less friction than pulling fixed wheels sideways. They have fewer wires and are also the least expensive.

Your kit may come with wheels, or you may need to buy them. The rubber-tired plastic wheel works well:

Figure 2.9 – Common inexpensive robot wheels

Figure 2.9 – Common inexpensive robot wheels

Figure 2.9 – Common inexpensive robot wheels

*Figure 2.*9 shows a common style for inexpensive robot wheels. There are many kits with them. They come in a few colors.

Another element to consider is the motors. With two wheels and a castor, only two motors are needed. It is tricky to solder or attach these wires to the small tags on motors, and poorly attached ones have a frustrating habit of coming off, so I recommend using those with wires attached or on modular connectors.

We spoke about different types of motors in Chapter 1*,* Introduction to Robotics; the most appropriate kind of motors for small robots are DC gear motors to balance the speed and torque in them. For a chassis around 200 mm, N20 or TT motor types are about the right size. Also, the chassis are compatible with the TT-size gearbox, so we’ll be using them:

Figure 2.10 – FIT0450 gear motors with encoders

Figure 2.10 – FIT0450 gear motors with encoders

Figure 2.10 – FIT0450 gear motors with encoders

The motors in *Figure 2.*10 fit the TT motor profile and include encoders. These are DFRobot FIT0450 motors. The encoders will let us detect rotations of the motor. We will explore encoders in Chapter 11, Programming Encoders with Python. These motors come with modular wire connectors and wires that fit them.

Avoid a complex or hard-to-assemble kit for your first robot build. The two-wheel drive and correct sizing also reinforce simplicity. I advise against a first robot being large or using unique and exciting locomotion systems, not because they are flawed, but because it’s better to start simple. There is a limit to this: a robot kit that is fully built and enclosed when bought leaves little room for learning or experimentation. An entirely premade robot may require toy-hacking skills to customize.

Related to simplicity is cost. You can buy robot chassis kits from around $15 up to thousands of dollars. Larger and more complex robots tend to be far more costly. For this book, I am aiming to keep to the less costly options or at least show where they are possible.

Making your own chassis or hacking RC toys can be very low-cost; however, this can be a little tricky for those less experienced with electronics. It is a fun option for the adventurous though.

You now have the design choices for a chassis kit, along with two wheels and two motors with encoders. I recommend the SparkFun Shadow chassis (ROB-13301) as they are not expensive, and are widely available on popular internet shopping sites:

Figure 2.11 – The SparkFun Shadow chassis

Figure 2.11 – The SparkFun Shadow chassis

Figure 2.11 – The SparkFun Shadow chassis

The robot in Figure 2*.*1 1 is the SparkFun Shadow chassis assembled with DFRobot FIT0450 motors and red wheels. Beware that some are sold with motors that do not have encoders; I recommend buying the chassis and motors separately.

We’ve chosen a chassis kit. It is a medium-sized one that doesn’t need large power handling. We can use these attributes to choose the motor driver. However, that also requires us to know a little about the main controller we’ll use with that motor driver. So, let’s explore the controllers and their I/O pins in the next section.

Our robot code will need to run on a controller at the center of the robot. Robots usually have a primary controller, and a computer of some kind. They may also have some secondary controllers, whereas some of the more unusual robots have many controllers. The controller connects all the other parts together and forms the basis of their interactions.

The interactions require connecting a controller to the components. For this, we need to get a better understanding of the I/O pins.

I/O pins are used for input and output from the controller. They give the controller the ability to connect to sensors, outputs, and motor drivers.

The number and types of I/O pins on a controller can limit what you can connect to a robot without using secondary controllers. You may also see the term General - Purpose Input / Output (GPIO), referring to pins with flexible functions instead of structured I/O ports like USB and HDMI. Controller I/O pins have different capabilities.

The simplest I/O pins, known as digital I/O pins, are only able to output or read an on/off signal, as shown below:

Figure 2.12 – A digital signal

Figure 2.12 – A digital signal

Figure 2.12 – A digital signal

The graph in *Figure 2.*1 2 shows a voltage level over time. Each voltage at a point in time shows a signal. The upper shaded region represents a digital logic high (1, True, On). The lower shaded region represents a digital logic low (0, False, Off). The controller will attempt to interpret any value as high or low. Digital I/O pins can be programmed to perform complicated tasks through signal timing, for example, communicating with a distance sensor.

Analog input pins can read varying levels, like the signal below:

Figure 2.13 – An analog signal

Figure 2.13 – An analog signal

Figure 2.13 – An analog signal

An analog signal is shown in *Figure 2.*13, another voltage-over-time graph. An analog pin can accept input from a sensor that produces a changing resistance or voltage. There are resolution limits to this, for example, an 8-bit analog input will read 256 possible values. These are sometimes called Analog - to - Digital Converter (ADC) pins.

On microcontrollers, like the Raspberry Pi Pico and Arduino, interrupts can be used to run a small bit of code as soon as a pin changes, allowing them to react rapidly to pin changes, such as high-speed encoder counts. Although systems like Raspberry Pi are more powerful, they have layers of software that mean you do not have access to hardware interrupts, which can make tight signal timing trickier.

Pulse Width Modulation (PWM) pins output a cycling digital waveform to represent continuous analog waveforms, as shown in the following graph:

Figure 2.14 – A signal approximated as PWM

Figure 2.14 – A signal approximated as PWM

Figure 2.14 – A signal approximated as PWM

The upper graph in *Figure 2.*1 4 shows a continuously varying signal. The lower graph shows the same information represented as PWM. PWM uses timing to represent information, keeping a constant frequency, but varying how much time a pin is at a high level in each cycle. The length of on-time versus off-time in a cycle, known as a duty cycle, changes to vary an output signal. In practice, PWM signals have far higher frequencies in relation to their changing signals. This is often used to control the speed of motors or the brightness of LEDs.

We will spend more time on PWM pins in Chapter 5*,Building Robot Basics – Wheels, Power, and Wiring, and Chapter 6,*Drive and Turn – Moving Motors with MQTT and Python.

Some I/O pins can be used to form data transmission lines or data buses. These will use multiple I/O lines for sending data, sending clock signals, and receiving data:

Figure 2.15 – Bus data transmission

Figure 2.15 – Bus data transmission

Figure 2.15 – Bus data transmission

Figure 2.1 5 shows a graph of data transmission of the word ROBOT as binary on an I/O pin, with another pin sending a clock signal. When the clock is high, the data on the data pin is ready to receive.

Common examples of these are serial (UART – Universal Asynchronous Receiver/Transmitter), I2C (Inter-Integrated Circuit, which can be pronounced as eye-squared-see or eye-two-see), I2S (Inter-IC Sound), SPI (Serial Peripheral Interface, pronounced as ess-pee-eye or spy) and USB (Universal Serial Bus). They are used to send control, audio, and sensor data between a controller and smart devices. We’ll use an I2C bus to connect to our motor controller in *Chapter 6,*Drive and Turn – Moving Motors with MQTT and Python.

We can use pins for digital or analog input and output, or part of a data bus. Many controllers allow the usage mode of pins to be configured in the software you run on them, but some capabilities are restricted to specific pins.

The main controller choice is fundamentally influenced by the capabilities you want a robot to have. While this can be changed on a robot, changing it later may require significant changes to programming, so it’s a decision worth making early. The controller must at minimum have programmable I/O pins.

Although it’s possible to use bare microcontroller chips with the right skills to create surrounding electronics and your own Printed Circuit Boards (PCBs), we’ll keep things simple in this book by using controller modules.

We want to start with easy-to-use controllers, which means we want great support documentation, easy programming languages, and well-packaged hardware.

We will use Wi-Fi and Bluetooth capabilities to make a robot headless and connect with smartphones.

We can program a robot in many languages; however, for someone just starting, I recommend using Python, so we’ll be looking at controllers that support Python as an option.

The robot in this book will be performing camera-based vision processing and speech recognition, so we need a controller capable of these. This will favor Single-Board Computers (SBCs) – those with camera connectors.

Cost is a factor but will be outweighed by the required capabilities. We will consider the power requirements and size of controllers. We also want to consider what kind of connections we’ll need. Since we’ve elected to use sensor modules, we can focus on digital pins, so analog pins are not needed.

Let’s use these criteria to filter a selection of robot controllers:

Figure 2.16 – A selection of controller modules

Figure 2.16 – A selection of controller modules

Figure 2.16 – A selection of controller modules

Figure 2.1 6 shows some controllers we can filter by our criteria:

  • SBCs (D): The Raspberry Pi is able to run Python, handle Wi-Fi, process speech, and perform visual processing on camera data. The Raspberry Pi’s relatively low cost and ease of use make it a clear choice over similar boards such as the BeagleBone, LattePanda, and NVIDIA Jetson Nano.
  • Python- enabled controllers (C): In this group are the ESP32 NodeMCU and Raspberry Pi Pico. These can both run Python and also have Wi-Fi flavors (with the Pico W). However, they will be very limited for camera visual processing or speech recognition.
  • Arduino b oards (B): The Arduino sparked off a wave of hobby microcontroller use. They are easier to use than a bare chip; however, they don’t have Wi-Fi and will not run Python.
  • A bare microcontroller chip (A): The PIC Microcontroller kicked off the hobby robotics community. However, our criteria suggested a whole board, not just a bare chip. It requires a separate programmer module and a user to build a PCB/board for it. This is not suitable.

Now we’ve filtered that choice, which Raspberry Pi do we choose?

All Raspberry Pi models mentioned can connect to a camera, Wi-Fi, and Bluetooth. Let’s see the capabilities that differ between Raspberry Pi models.

Raspberry Pi is powerful enough to handle some visual processing tasks, such as facial recognition and tracking objects, with later models being able to perform this faster. The same can be said for voice recognition tasks too. It is for this reason that the faster 4B, 3B+, and 3A+ models are recommended. The Zero 2 W model is a little slower, and although the system will still work, the speed and low memory may be frustrating. A Pi 4B can have up to 8 GB of RAM, whereas a Zero 2 W or 3A+ can only have 512 MB.

These are all quad-core ARM-based CPUs.

Throughout this book, we will be using Wi-Fi to program the robot, drive it, and start code running on it.

All Raspberry Pi models have USB and HDMI ports. We don’t plan on using them in this book, although they are good for debugging if you lose contact with a robot. With that in mind, having an additional screen and keyboard available is recommended.

There are a few models of Raspberry Pi, some of which are suitable for robotics. There may be newer models released, and the availability of different Raspberry Pi models will mean a builder needs to adapt.

A Raspberry Pi Pico is not the same as a Raspberry Pi – it is a microcontroller that runs Python but will not be suitable for vision and speech processing programs.

Let’s look at a few Raspberry Pi models in the following figure:

Figure 2.17 – Raspberry Pi models – 4B, 3A+, and Zero W

All the pictured models in *Figure 2.*17 have Wi-Fi and Bluetooth. The Raspberry Pi I/O pins support many data bus types and digital I/O.

Let’s look at each of these models in a little detail:

  • Raspberry Pi 4B (A): This is among the faster models. It is more expensive and uses more battery power than the others. The lower memory versions are far cheaper and quite useful for robotics.
  • Raspberry Pi 3A+ (B): This is the favored controller as it provides an excellent balance of size and power. It is fully capable of visual processing through a camera. It’s not quite as fast as the 4B, but quick enough for our purposes.
  • Raspberry Pi Zero 2 W (C): This is an inexpensive, light alternative to the other Raspberry Pi models. Cameras and speakers are still supported. Unless you buy the H option, you will need to solder on headers. It performs tasks slower than on a Raspberry Pi 3 and 4.
  • Raspberry Pi 5 (Not pictured): At the time of writing, this was the latest and fastest Raspberry Pi. Its suitability for robots is yet to be assessed, but it may present some challenges for powering unless building larger robots.

Note that you will need a USB-C power connector for the 4 and 5. I recommend the Raspberry Pi 4B.

When building the robot, we will use a subset of the connections the Raspberry Pi has to offer. As we connect sensors and parts to the Raspberry Pi, we will cover the connections in detail. The following diagram shows the connections in use:

Figure 2.18 – Raspberry Pi connections

Figure 2.18 – Raspberry Pi connections

Figure 2.18 – Raspberry Pi connections

In the preceding diagram, the used connections are labeled.

First, we will be using the power connector, labeled Power at the bottom left of the diagram. This will either be a micro-USB or USB-C similar to those on phones. We will use this for powering the Raspberry Pi. We can plug USB battery packs into this port if they can provide the correct amount of power. Raspberry Pi recommends 2.5 A power supplies, although anything over 2.1 A will suffice.

The port labeled C amera is a Camera Serial Interface (CSI) port for the Pi camera, which we will attach when preparing to do visual processing.

We will be using the Micro S D card slot under the Raspberry Pi to run our code.

The large connector across the top of Figure 2*.1* 8 is the GPIO port. We will see how we can use this to power motors. For a closer look at this port, see pinout.xyz. This is where we will connect most of our sensors and motors. External devices can be attached to SPI, I2C, and Serial data buses, or digital I/O pins.

The 5 V and 3.3 V pins are used for power, along with the pins marked GND. GND is an abbreviation of ground, which is the equivalent of a minus terminal on a battery or power supply. The 5 V pin can be used to supply the Pi with regulated power from batteries. 5 V and 3.3 V can be used to supply small electronics or sensors.

The Raspberry Pi has data buses that can be enabled through configuration, or the pins can be used as general digital pins when the data buses are turned off.

The pins marked SDA (Serial Data) and SCL (Serial Clock) in Figure 2.18 are an I2C data bus. We use this for sensors and motor control boards. Instructions are sent over this port.

The other pins that are numbered, without a specific word or shading type, are GPIO pins. These are used for digital inputs and outputs with servo motors, encoders, and ultrasonic sensors.

Raspberry Pi pin numbers

Why are the numbers mixed up? The numbers used in most Raspberry Pi documentation are BCM numbers, which correspond to pins on the main Broadcom chip. Use Figure 2*.*18 or https://pinout.xyz/ for reference.

Let’s use what we now know to choose the main breakout.

A main breakout is a circuit board you use with a controller to deal with connected devices. It can be simply a motor driver attached to some Raspberry Pi pins, but we might want more out of it with regards to the sensor, servo motor, and other connections.

As we did for choosing the controller, let’s make some criteria, and filter some breakout and motor driver options by them.

Breakout boards come at different integration levels. The least integrated components need point-to-point wiring on a breadboard between them. This can result in a lot of wiring and take up a lot of space, and more wires often mean more ways a robot can go wrong. It may be the most flexible option.

The other end of the scale is breakout boards that integrate everything – motor controllers, servo motor breakouts, and sensors on board. Because sensor positioning is important, it’s worth finding a balance where a lot of functions are integrated, but sensors can be added to extend it and at locations convenient to the robot builder.

Raspberry Pi HATs (Hardware Attached on Top) are breakouts designed to fit over the Raspberry Pi, plugging into the GPIO port to conveniently connect the Raspberry Pi with motors or sensors.

HATs use GPIO pins for different purposes; for example, audio HATs will use the I2S pins for audio interfacing, but some motor controller HATs use the same pins to control motors instead. Using these HATs together can be problematic, so be aware of this when using multiple HATs or specific buses.

Although a HAT plugs into all pins, this doesn’t mean they are all used. Only a subset of the pins is usually actually connected on a HAT. The website https://pinout.xyz allows you to look up popular HATs and find out what pins they use.

We want to have I/O pins and an I2C bus available. Some breakout boards use too many I/O pins and do not leave enough pins available for your usage. A good choice for flexibility is to choose one with the pins you’ll need to be exposed and use smart devices such as I2C motor controllers to keep many pins free.

Large motors need lots of power, which may need either high current (in amps) or high voltage (in volts) to power them. Underspecifying this can lead to a robot stalling, or worse, overheating and even catching fire.

Do not connect a Raspberry Pi directly to DC motors, as they require high currents that can destroy GPIO pins, so our choice will need a motor driver.

For smaller robots, oversize controllers come with obvious size and weight issues but also may have lower voltage limits that are unsuitable for the motors in use. For a robot like ours, around 1 to 1.5 amps per channel is good. The motors will not accept much over 7 volts, which makes larger motor drivers unsuitable.

Good motor drivers should also include capacitors – components that reduce problems associated with motor noise and spikes. Motor noise can cause nearby servo motors to jitter, or bus signals to be confused.

As you choose boards for a robot, note that some come as kits themselves, requiring you to solder parts on them. A small header is going to be a quick and easy job, suitable for first-time soldering. A board that comes as a bag of components with a bare board will take a chunk of an evening and could require debugging itself. An example is the Adafruit Stepper Motor HAT mini kit.

Soldering is an essential skill for robot building. We try to keep it light for a first robot, so for this book, I mostly recommend pre-assembled modules. If you want to get into soldering, I recommend https://learn.adafruit.com/adafruit-guide-excellent-soldering.

Closely related to soldering are the connectors for the motor. The connectors on motors and sensors may not match those on the board.

Connectors on a board may require soldering or crimping. Screw-type connectors are easy to work with due to their flexibility. However, in later chapters, we’ll see how to use connectors pre-crimped with wires, known as pig-tails, and connect them together to make wiring for our robot.

Let’s summarize our criteria:

  • We’ll have two wheels, so we want to drive two motors.
  • We’ll use a Raspberry Pi camera, so the camera port should be accessible.
  • We’ll connect I2C devices, so the I2C ports should be usable.
  • The encoders and sensors need 3 V power supplies, so we need access to ground and voltage pins for those too.
  • We need four input pins for the motor encoders.
  • We plan two servo motors for pan and tilt, so we’ll need to drive two servo motors too.
  • We want a starter-friendly option, so only a little soldering and nothing complicated.
  • It should have good software support – which favors branded items.
  • It should be easy to get hold of – branded items tend to be repeatable, and generic items can vary a great deal.
  • The motor voltages should be compatible with the FIT0450 motors, which are expected between 3 V and 7.5 V.
  • Larger motors and loads need much larger motor controllers with heatsinks. However, for this small robot, we don’t need anything huge.

Let’s look at some options, and use our criteria to filter them:

Figure 2.19 – Main breakout  choices

Figure 2.19 – Main breakout choices

Figure 2.19 – Main breakout choices

The sample here represents a number of options for a Raspberry Pi robot breakout. It is worth pointing out that no breakout is yet perfect, and all come with trade-offs. All the boards above will drive two motors and are well supported. So, what other criteria do we trade off?

  • The L298N breakout (A) will only drive motors. You could make a breadboard or solder a PCB for I2C connections, servo motors, and encoder feedback. This requires a lot of work. These boards come in many flavors.
  • The CamJam Edukit motor controller (B) has access to I2C and four I/O pins for the motor encoders. It would still need another wiring board for servo motors, and enough ground and voltage pins to be available for our other devices.
  • The Adafruit Stepper Motor HAT (C) has the ability to drive DC motors and servo motors and support the connections we want. However, you’ll need to solder on a header, servo motor breakouts, and pin headers for the ground and voltage access. If you are new to soldering, this would be a brave choice.
  • The RedRobotics RedBoard+ (D) is a HAT with two servo motor breakouts and can drive two motors. However, for 3 V connections, you’ll need a wiring board to make enough available. The bigger issue is that with a power input minimum of 7.2 V, this board provides too high a voltage for the FIT0450 motors. This is very suitable for larger motors though.
  • The Adafruit CRICKIT for Raspberry Pi (E) is extremely capable; however, without additional wiring, it does not expose I2C ports for us to connect sensors.
  • The Pimoroni Inventor HAT Mini (F) lets us connect I2C devices via that QW/ST connector on the side, including their power. It has servo breakouts, and the motor connections come ready and equipped with encoder pins. There’s a trade-off: we will need to solder adaptor boards to connect our motors to those motor A and B connectors on it.

Based on the trade-offs seen here, I recommend Pimoroni Inventor HAT Mini to follow along. You could substitute for another board; however, you will need to adapt code close to I/O pins where things are different.

We’ve now got the main breakout, chassis, and controller chosen. We also need to decide about how we will power this.

The robot needs power for all its parts. We must consider two major power systems: the power for all the digital parts, such as the Raspberry Pi and sensors, and then the power for the motors.

Motors use a lot of power, often being one of the larger consumers of a robot, with servo motors sometimes greedier than the drive motors. Motors can also need higher or different voltages from the Raspberry Pi. However, with the choice of motors we have, they will comfortably run at the same voltage.

A power system will need to either provide two different power sources – one for the Raspberry Pi and one for the motors or be a system large enough to cope with all the demands. A system that cannot handle all the power may experience voltage drops and cause Raspberry Pi resets, along with SD card corruption, so it must be avoided.

We will also need to make trade-offs on the weight and size of the power system versus how much play time we want between charges.

There are a few power systems for a robot:

  • Dual batteries: The motors and the rest of the robot have entirely separate sets of batteries, ensuring that their power is independent. This takes up a lot of space and weight but can be the safest option.
  • Regulators: Use aFigure 2.20 – Voltage regulators Figure 2.20 – Voltage regulators
  • A regulated battery pack: There are battery packs that have a regulator built in, with USB power packs being a common kind:Figure 2.21 – USB power banks Figure 2.21 – USB power banks Figure 2.21 – USB power banks These save space and complexity. Look for those that can handle 5 V 3 A output. In the two pictured, the red one in the lower part of *Figure 2.*21 can handle only 2.4 A, which may not be enough. Check that the USB power bank comes with the cable to connect the Raspberry Pi – a Raspberry Pi 3 (A+ and B+) requires a USB micro connector, and the Raspberry Pi 4B requires a USB-C connector. Note that most of these will switch off when being charged.

Lithium polymer batteries can provide a lot of power and, when used with a UBEC, could be capable of powering the Raspberry Pi and motors. However, they must be carefully managed and handled to remain safe. I do not recommend them for beginners because they can be a fire risk if incorrectly wired.

The motor board we have suggested can be connected in one of two ways. It can either share power with the Raspberry Pi, with the Raspberry Pi powered through a regulated option above, or it can be separately powered, although this requires soldering and cutting a track.

We will use the simplest option, a power bank with 3 A output, but keep the possibility of a separate power supply as an option should we need it. I’ve chosen one of the pictured battery units, the NanoWave 3 5000 mAh (UBP0119A) power bank. It can output the required 5 V at 3 A and is fairly compact.

What about sensors?

We saw sensor types in Chapter 1*,* Introduction to Robotics; we now need to consider sensors specific to this robot. We will dive more into the sensors in their respective chapters and why they have been chosen. Here are the sensors we’ll use:

  • The VL53L5CX distance sensor array, so we can detect many distance points, like LiDAR but at a low cost. The Pimoroni breakout for the VL53L5CX is among the smaller and cheaper models.
  • The Raspberry Pi camera for visual processing.
  • For an IMU, we will use the BNO055 as it will save a lot of programming complexity over alternatives.

We have now made part selections. We know which chassis, controller, breakout, and battery configurations we will use. We have also seen the size of the motors and wheels. Before we buy all this, we should check to see that it will all fit. In the next section, let’s learn about test-fitting robot parts.

A test fit is an important stage before ordering parts as it helps a builder be more confident that components will fit where you’d expect. This step saves you time and money later. While I include the dimensions here, you should check the referenced resources to get into the habit of collecting such specifications for your own designs.

We’ll start by using a search engine to get dimensions. The search term “Raspberry Pi 4 Dimensions” will show approximately 86 mm by 57 mm. The nearest millimeter is good enough. You can find the NanoWave 3 battery dimensions on the Pimoroni website, which come to 76 mm by 35 mm. The SparkFun Shadow chassis dimensions are 192 mm by 126 mm, with a height of 44 mm. Given that the battery’s long dimension is 76 mm, we can be confident it will fit on the chassis.

Let’s turn the ideas into a test fit – in this case, using a drawing program. Use the dimensions to make your own drawing:

Figure 2.22 – Robot test fit sketch

Figure 2.22 – Robot test fit sketch

Figure 2.22 – Robot test fit sketch

The sketch in Figure 2.2 2 was made in Inkscape, but it could be pen and paper, movable bits of card, or your favorite diagram-drawing tool. The important part is that you type in the dimensions of each part and label them. Because the Shadow chassis has upper and lower decks, I’ve drawn both. The test fit does not need much detail, but you can add more detail if some areas appear too close. For example, you probably don’t need bolt holes and corner detail, since everything fits quite loosely in this design.

We need to mount the distance sensor on the front of the robot. Pimoroni sells a black plastic angled bracket, COM3600, in a pack of 10, which we can use to mount the sensor.

We now have a reasonable expectation that these parts will fit together; it’s time for you to go and buy your parts and chassis. See Appendix A*: Shopping* for Robot Parts for a robot parts shopping list.

In this chapter, you’ve seen different robot parts from the view of planning a robot, and using a block diagram as a plan, you started to visualize how you’d combine those blocks to make a whole robot. You’ve sketched your robot ideas and upgraded them with computer drawing tools for a neater version of the same diagram.

You’ve had a quick tour of how controllers use analog, digital, PWM, and data buses to communicate with the other devices connected to them. Following on from this, you’ve planned the robot we will build in this book. You’ve seen more of what the Raspberry Pi is and which connections on the Raspberry Pi we will use.

In the next chapter, we will look at Raspberry Pi OS, the operating system used on the Raspberry Pi in our robot, and how to make it headless so we can operate over Wi-Fi.

  • Try creating a block diagram for a different robot, thinking about inputs, outputs, and controllers. Consider what chassis, power, and parts it needs. Use online vendors to find suitable parts.
  • Are the Raspberry Pi 4B and 3A+ still the most recent versions? Would you use another model, and what would be the trade-offs?
  • For your alternative robot, consider whether there is a suitable combination of Raspberry Pi HATs that fits your design. Use resources such as https://pinout.xyz to check that their pin usage is compatible.
  • What power systems might be suitable for the Raspberry Pi and the output devices in your new design?
  • Consider how the new robot parts will fit together. Make a test-fitting sketch for your new robot parts, checking that they fit using part dimensions.

Please refer to the following to get more information:

  • Raspberry Pi Sensors, Rushi Gajjar: Learn how to integrate sensors into your Raspberry Pi projects and let your powerful microcomputer interact with the physical world.
  • Make Sensors: A Hands-On Primer for Monitoring the Real World with Arduino and Raspberry Pi,Tero Karvinen,Kimmo Karvinen,Ville Valtokari,Maker Media, Inc.**, https://www.makershed.com/products/make-sensors-1: Learn how to use sensors to connect a Raspberry Pi or Arduino controller with the real world.
  • Make: Electronics: Learning Through Discovery,Charles Platt, Make Community, LLC*,*https://www.makershed.com/products/make-electronics-3rd-edition-print: This is a useful resource if you want to find out more about electronic components and dive deeper into the individual components.