tinkering with lego and microbits - stem

12
CCITE The Cambridge Centre for Innovation in Technological Education Professor Adrian Oldknow [email protected] http://ccite.org Tinkering with Lego and micro:bits Adrian Oldknow August 2019 Many schools and homes have bits of Lego which can be used to build objects which we can control using BBC micro:bits. In this article we will look at some of the useful gadgets which can help us make the connections. The simplest device to put something in motion is called a servo motor. This comes in two varieties. The positional one just turns to a given angle usually between 0˚ and 180˚ or 270˚. This is often found in remote control boats, planes and cars where it can turn the rudder or steer the wheels. The other is a continuous servo which works like an ordinary electric motor and can run forward, back or stop. Most servos need more than the micro:bit’s 3.3V power supply, so that’s when we need to have a look at some of the other add-ons available. Here is a simple example of some code to check and calibrate a rotational servo such as the SG90 which costs as little as £7 for 5! . This works, just, with a simple micro:bit and a standard 2xAAA 3V battery pack. The piece of technical Lego is attached to the `horn’ of the servo with a bent paper clip. My micro:bit is fitted with the £3 basic:bit from Cool Components.

Upload: others

Post on 28-Feb-2022

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Tinkering with LEGO and Microbits - STEM

CCITE

The Cambridge Centre for Innovation in Technological Education Professor Adrian Oldknow [email protected] http://ccite.org

Tinkering with Lego and micro:bits Adrian Oldknow August 2019

Many schools and homes have bits of Lego which can be used to build objects which we can control using BBC micro:bits. In this article we will look at some of the useful gadgets which can help us make the connections. The simplest device to put something in motion is called a servo motor. This comes in two varieties. The positional one just turns to a given angle usually between 0˚ and 180˚ or 270˚. This is often found in remote control boats, planes and cars where it can turn the rudder or steer the wheels. The other is a continuous servo which works like an ordinary electric motor and can run forward, back or stop. Most servos need more than the micro:bit’s 3.3V power supply, so that’s when we need to have a look at some of the other add-ons available. Here is a simple example of some code to check and calibrate a rotational servo such as the SG90 which costs as little as £7 for 5! .

This works, just, with a simple micro:bit and a standard 2xAAA 3V battery pack. The piece of technical Lego is attached to the `horn’ of the servo with a bent paper clip. My micro:bit is fitted with the £3 basic:bit from Cool Components.

Page 2: Tinkering with LEGO and Microbits - STEM

This has 3 sets of 3 pins connecting to P0, P1 and P2 as well as 3V and GND. It also has an on-board buzzer. Press button A to increase the angle by 10˚ and button B to decrease it. Can you use this as the basis of a level-crossing barrier, say? You could also explore creating walking or wriggling robots using these motors. Another device for connecting 3-pin servos comes from Kitronik. The £11.40 Servo:Lite board is described here. This also bolts on to a micro:bit and has a 3xAAA battery pack. It also has a neopixel strip with 5 leds. A small 360˚ continuous servo motor costs about £5. So here is an example of a buggy made using technical Lego where the servos are a snug fit into the chassis. To power a pair of these you will need external batteries. The easiest way is to buy an add-on board such as the £6 Ring:bit from Cool Components. This bolts on to the micro:bit and has a 3xAAA battery pack on its reverse. The value passed to the servo is a number between 0 and 180. 90 corresponds to being still, 180 is full speed forward and 0 is full speed backwards. The way I have fixed the motors has them pointing in different directions. See if you can work out how the following program works, and add extra functions to move backwards and turn right.

Page 3: Tinkering with LEGO and Microbits - STEM

To make life a bit easier you can install a MakeCode package which gives you some extra commands to make controlling the motors simpler. Use Advanced and Extensions, then search for “Ringbit”. This will show an image of a built RingbitCar kit. Select this and you will install the additional commands to control the motors. There is one company, KittenBot, which makes both positional and continuous servos which are Lego compatible. They cost £17.35 for 4. The two grey ones are rotational 270˚ servos with 3-wire connections. The two red ones are continuous servos with 2-wire connectors. In each case they can be controlled directly by Pin commands in MakeCode. Here is a sample program to use a £12.80 Kitronik Motor Driver Board. The board has a slit into which a micro:bit fits. It has a 2-hole terminal for connecting an external power supply such as a 3xAAA battery holder.

We connect one red servo to the two Motor 1 terminals, and the other to the two Motor 2 terminals. Motor 1 is connected to pins P12 and P8, motor 2 is connected to pins

P16 and P0.

Page 4: Tinkering with LEGO and Microbits - STEM

The micro:bit is powered by a 3V output from the board, as with the Ring:bit and Servo:Lite boards. All we have to do now is to build a chassis and connect some wheels. An alternative to buying a ready made servo or motor driver board is to build your own using a breadboard to connect up the electronic components needed for a motor driver. Fortunately there is a ready made integrated circuit IC (or chip) designed for the task. Known as a Dual H Driver, the Texas Instruments SN754410 chip costs £1.92 from Hobbytroniks. The instructions are here. On the left is the diagram showing each of the 16 pins wired to drive a pair of motors. On the right is actual wiring from the website.

My test rig on the left has a 3xAAA battery box to power the two motors and a 2xAAA box to power the micro:bit.

Page 5: Tinkering with LEGO and Microbits - STEM

A small breadboard like the one shown costs about £1.50. An adaptor for the micro:bit to connect to the breadboard costs around £5. A set of 40 male-to-male jumper leads costs around £3.60.

If you would rather use a motor than a servo, then Pimeroni sell £5 small powerful motors with a variety of gearing as well as simple adaptors (£3 for 4) to connect them to Legotechnic axles and rods. Now we have explored building mechanisms and vehicles usin£g Lego as the building bricks for chassis, axles and wheels, we can explore how to use a second micro:bit for remote control using the joystick principle. In an aeroplane’s cockpit there are many controls and displays. One of the most important control is the joystick, or just “stick” for short. The pilot pushes the stick forward to dive and pulls it back to climb. Pushing the stick left banks the plane left, and pushing it right banks the plane right. The micro:bit has sensors to detect its forward/back movement, called “pitch” and its left/right movement, called “roll”. The Input menu has blocks to read sensors for the pitch and roll angles, which vary from -180˚to 180˚. We could use the analogue values of the pitch and roll angles to change the speeds of

the motors M1 and M2. Or we could choose a `threshhold’ angle such as ±20˚ and

convert each of the actual angles into a digital code -1, 0 or 1 to show in which way

the micro:bit is tilted. If the pitch angle Pa is bigger than 20°, say, we can set the

variable Pi to 1 to show it is titled right. If we add 20 to the pitch angle and divide by

40, ignoring any remainder, then an angle between -20° and 20° will map onto zero.

The Math menu has another useful function called Constrain. So we can set Pi to the

value given by the formula: Pi = constrain((Pa + 20) ÷ 40, -1, 1) where ÷ stands for

`integer divide’. Similarly: Ro = constrain((Ra + 20) ÷ 40, -1, 1).

Page 6: Tinkering with LEGO and Microbits - STEM

We can give visual feedback on the micro:bit by turning on one of 5x5 array of LEDs

to show the tilt direction, corresponding to compass points N, NE, E, SE, S, SW, W,

NW. We can then use the radio to send the values Ro and Pi to the micro:bit

controlling the buggy.

This will need to tell each of the Left and Right motors

whether to turn forward, backwards or stop. So we can

have variables Le and Ri which hold -1, 0 or 1. Here is a

diagram and a table showing how the points of the

compass are detected by Ro and Pi, and will be reached

by setting appropriate values for Le and Ri. On the

diagram the values in the brackets correspond to (Le, Ri).

For example to head straight on in the direction ZN we

need to set both motors to forward (1, 1), but to turn to right in the direction AE we

need to set the left motor to forwards and the right motor to backwards (1, -1). To

turn gently left in the direction Z to NW we can stop the left motor and just drive

forward with the right one (0, 1).

Page 7: Tinkering with LEGO and Microbits - STEM

So we can calculate the Left and

Right motor values Le, Ri again

by some simple maths on the

pitch and roll values Pi and Ro.

We can use the `constrain’

function again to convert the 2

and -2 values into 1 and -1.

On the MakeCode simulator the top micro:bit has been pitched forward and rolled right to the NW position (1, 0). That movement is achieved be stoppng the left motor and running

Direction Pitch Roll Left Right Pi+Ro Pi-Ro

N 1 0 1 1 1 1

NE 1 1 1 0 2 0

E 0 1 1 -1 1 -1

SE -1 1 0 -1 0 -2

S -1 0 -1 -1 -1 -1

SW -1 -1 -1 0 -2 0

W 0 -1 -1 1 -1 1

NW 1 -1 0 1 0 1

Z 0 0 0 0 0 0

Page 8: Tinkering with LEGO and Microbits - STEM

the right motor forward (0, 1). To complete the program we just have to use the Le and Ri values to send appropriate signals to the Left and Right motors. The data sheet for the Kitronik motor driver tells us what motions the pins control.

I have chosen not to use the “coast” motion, but to use “brake” instead. So here are the values we have to “digital write” to pins P8 and P12 for the Left motor and pins P0 and P16 for the Right motor: The MakeCode Simulator shows that when the Sender is set to SE then the pins P0, P12 and P16 are set to high, while P8 stays low. We will use variables v0, v8, v12 and v16 to hold the values to right to pins P0, P8, P12 and P16. The functions “LeftPins” and “RightPins” set up the values v0, v8, v12 and v16. The function “Pins” writes these to pins P0, P8, P12 and P16.

Point P8 P12 P0 P16

N 1 0 1 0 NE 1 0 1 1

E 1 0 0 1 SE 1 1 0 1

S 0 1 0 1

SW 0 1 1 1 W 0 1 0 1

NW 1 1 1 0 Stop 1 1 1 1

Page 9: Tinkering with LEGO and Microbits - STEM

Once we have defined our three functions we just need to include calls to each of them at the end of the “on radio received” block. We can download the same program to both the sending and receiving micro:bits. We may need to do some adjustments though since the right and left motors have been set up in opposite directions! As it turns out we got it right first time! To complete the kit it will be convenient to give the sending micro:bit its own bolt-on coin battery holder such as the Kitronik £5 M1:power which comes with a battery, on/off switch and buzzer included. In this example we have used 2 micro:bits, 2 continuous servo motors, a motor driver board, a bolt-on power unit, a 3xAAA and a 2xAA battery holder plus some Lego bricks, axles and wheels. If you already have the micro:bits then the additional bits of kit cost around £30. If you already have some Lego electronic components there is a good chance that someone as found a way to get them to work with a micro:bit. A nice example using the Lego Power Functions Infra-red Sender is here. Another one using Lego motors is here. We will finish with a couple of project ideas for tinkering. Project 1 – using servos to crawl, walk or flap – biomimicry

We can use positional servos to simulate the motion of animal

limbs and bird/fish tails. Lego makes a good medium for building

the parts of the simulated body. Here is my simple test-bed

using a pair of 180° servos controlled by a suitable driver board

such as the ELECFREAKS ring:bit or Kitronix servo:lite.

Page 10: Tinkering with LEGO and Microbits - STEM

The small wheels at the

back are free to turn on an

axle. Each servo has a

Lego piece attached with a

large bent paper clip to

make the legs. The servos

are wedged tight between

Lego bricks and held in

place with an elastic band.

The ring:bit has the

micro:bit bolted to it and

carries a 3xAAA battery

pack. It is also wedged

tight in place. The code

just uses the Servo write

pin command from the

Pins menu.

The variables L1, L2, R1 and R2 define the range of angle each leg can turn. The

simulator shows the servos side by side so that they appear to be rotating in the

same direction. But on the chassis, the servos move in opposite directions which

model a stiff-legged walking movement. When button A is pressed you have 2

seconds to position the creature by hand before it takes 20 paces, alternating which

Page 11: Tinkering with LEGO and Microbits - STEM

leg moves forward and which moves back. Now you have the basic idea of a gorilla-

like waddle, you can use additional

servos to create additional joints

such as ankle, knee and hip, or

shoulder, elbow and wrist. Kitronik

has a £20 All-in-one Robotics board

with connections for eight 3-pin

servos and for 2-pin motors, as well

as for an external battery box. The

micro:bit fits into a slot on the

board and receives 3.3V power

from the board. If you look around

the internet you can find a great

many interesting ideas for robotics.

The first two pictures are of a 4-

legged crab-like vehicle using 2

servos for each leg.

The two-legged version is a

research project described here.

The six-legged one, called a

hexapod, is described here.

Meccanoid is a self-assembly robot

with speech recognition. This rolls

along rather than walks. The

servos control the arm and head

movements. Channel 4 TV uses

computer graphic effects to create

a virtual two-legged metal robot

called “it’s nice that”.

Page 12: Tinkering with LEGO and Microbits - STEM

If you have access to a 3D printer you can also find many ready-made designs for

useful Lego adapters for electronic components like motors and sensors.

Project 2 – hacking a radio-controlled car

An alternative to assembling your own motors

and mechanisms is to find or buy a cheap

electronic radio-controlled car and to replace its

controller with a micro:bit and suitable motor

driver board. Full instructions are given on the

Micro:bit Foundation’s website for the project

called “rc-car”.

The Computing At School group’s website carries

a nice posting about how a school used this as the

basis of a STEM Club challenge. The teacher,

Joanna Wakefield, has posted some nice

resources she used. The basic vehicle is a rugged

jeep which costs just £10 including the RC

controller. Below is a picture of the car with its

bodywork unscrewed to show the motors which

drive each of the rear wheels as well as the servo

which steers the front wheels. On the right are the

two winning cars

behind the stripped

down jeep which

carries a micro:bit

in the Kitronix

motor-driver board.