Building a Raspberry Pi powered robot...

During this current lockdown we have decided to have a go at robotics! Using a raspberry pi and the CamJam EduKit 3.

If you would like to know more about the raspberry pi please visit the what is a raspberry pi page.

The Parts

These are the parts for building our robot. They include from right to left and down.

  • A battery pack – to power the motors
  • Two motors
  • Two wheels
  • A distance sensor
  • A line following sensor
  • A breadboard - for easy wire connection
  • Double sided sticky pads
  • A ball caster
  • A motor controller PCB
  • Resisters
  • A Raspberry Pi - The brains
  • Jumper cables

Building our robot

...

First we used the double sided sticky pads to attach the motors to our chassis (an old google nest mini box)

...

Then we attached the wheels

...

Next we attached the castor which is a big metal ball baring for the front wheel

...

We made some holes and pushed the motor wires through. Now the robot is ready for its inners

...

Jack first wired the motors to the motor controller

...

Then the battery pack to the controller

...

The controller slots onto the Raspberry pi GPIO (General-Purpose Input/Output) header

...

We need to add a power supply for the Raspberry pi itself. The battery pack wired to the motor controller is just to power the motors. We used an old power bank to power the pi, its a bit on the large size, we hope it will help give some weight to the robot to help the tyres grip.

Coding

Now for the coding. We power up the raspberry pi and connect to it remotely using a MacBook. We use a programming language called Python. It looks like this:

  1. import time # Import the Time library
  2. from gpiozero import CamJamKitRobot # Import the CamJam GPIO Zero Library
  3. robot = CamJamKitRobot()
  4. robot.forward()
  5. time.sleep(1) # Pause for 1 second
  6. robot.backward()
  7. time.sleep(1)
  8. robot.left()
  9. time.sleep(0.5) # Pause for half a second
  10. robot.right()
  11. time.sleep(0.5)
  12. robot.stop()
The first two lines of the code import libraries. Libraries are pre-written code that enables you to write simpler code, like the code below. You can simplify code by writing functions. Functions are like storing many instruction in one word. This is a bit like how some of our words work, for example if you tell someone you’re going shopping, you know that word means a set of instruction, like making your way to a shop, finding things you want, paying for them, putting them in a bag and bringing them home. You do the same with code, to simplify it for later use, make one word mean/do many things. For example robot.left() tells the Pi to turn the power off on pin 9 on on pin 10 off on pin 8 and on on pin 7 making one wheel turn one way and the other the other way.

First Test

With a little trial and error we worked out to run the motors in the right order and for the right amount of time to get the robot to the front door.

Adding the line sensor

...

Jack attached one end of the jumper cables to the line sensor.

...

He then attached the end of one of those wires to the bread board.

...

He used another to connect the bread board to a pin on the Raspberry Pi. The breadboard is a way to connect two wires together as long as they are in the same column (inline vertically).

...

After typing up the code for the line sensor we ran some tests. Every time the sensor went over the black line “Line seen” was printed to the computer screen and when taken away from the line “Line not seen” was printed to the screen.

...

We then made a hole for the wires so we could attach the sensor to the bottom of the robot. We had to modify the wiring a little to give us more cable to reach underneath.

...

Here it is, our robot now has a line sensor!

Next we checked to see if the sensor works in that position. We originally had it placed further forward and it didn’t work, but as you can see it now works fine.

Adding the ultrasonic distance sensor

...

This is the ultra sonic distance sensor. It sends out a sound that’s too high in frequency for our ears to here. It then waits for the sound to bounce back off whatever is in front of it and works out how for away the something is by how long it takes for the sound to bounce back.

...

We started by connecting four jumper wires to the sensor, inserting them into the breadboard along with wires from the right pins on the Pi.

...

For this sensor to work we had to restrict the flow of power going to the sensor. To do this you need to use resisters.

After typing up the code and running it here is the result. The sensor measures the distance in centimetres. If you watch you can see when Jack puts his hand in front of the sensor.

...

Now our robot has eyes!

Well technically it has a mouth and an ear.

Line following tests

We didn’t have much luck with getting the robot to follow the lines as can be seen in this early test.

We did manage to capture one successful test! Unfortunately any tests after this one failed.

We think there are a few issues causing the robot to struggle to follow the line. First there was the weight issue, the large power bank was making it difficult to get the robot to move slowly and precisely. We think this is also true for the motors, we just don’t think they are quite up to the task. Once we removed the power bank it was a challenge to get the robot to make slow turns. We also think the chassis (the box) doesn’t help and the position of the line sensor. This isn’t the end for the line following though….We have ordered an exciting new chassis and slower more precise motors! The new chassis is smaller so I think our robot will be built using the Raspberry Pi Zero as it is a lot smaller and will take up less space. So exciting stuff to come, check back soon!

In the mean time...

Whilst we wait for the new parts we decided to move on with the distance sensing. After tweaking the code a little we got it to work! We noticed the robot doesn’t do well when approaching a surface at an angle. This will be because the sound it sends out wont be bouncing back to it but around it. The current motors also hindered this part of the project with lack of precision.

Introducing our new and improved robot!👏

...

These are the new parts for our robot. Including a chassis with caterpillar tracks and new more precise motors.

...

We first had to solder some wires to the motors. Solder is a type of metal that melts easily and is used to join electrical components.

...

We then built the chassis.

...

Put the motors in place in the chassis.

...

We then added the tracks and flipped it over and screwed on the mounting plate.

...

This time we made a custom casing for our robot out of plastikard (sheet plastic).

...

Here is the case all glued together minus the top piece.

...

Here it is! We think he looks a lot better. He has a lockdown haircut, but don’t we all!

But how well does our improved robot cope with the previous tests??

Line following test

Distance sensor test

Well I think you can agree it copes with them with ease, we’re made up with it 🙂