Micro:bit Hand Biting Crocodile Tutorial

1.Servo

1.1 Introduction

Servo is a position control rotary actuator. It mainly consists of a housing, a circuit board, a core-less motor, a gear and a position sensor. Its working principle is that the servo receives the signal sent by MCUs or receivers and produces a reference signal with a period of 20ms and width of 1.5ms, then compares the acquired DC bias voltage to the voltage of the potentiometer and obtains the voltage difference output.

In general, servo has three wires: brown, red and orange. The brown wire is grounded, the red one is a positive pole wire and the orange one is a signal wire.

The rotation angle of servo is controlled by regulating the duty cycle of PWM (Pulse-Width Modulation) signal. The standard cycle of PWM signal is 20ms (50Hz). Theoretically, the width is distributed between 1ms-2ms, but in fact, it’s between 0.5ms-2.5ms. The width corresponds the rotation angle from 0° to 180°.

image-20230620134023059

1.2 Specification

Working voltage: DC 3.3V〜5V

Operable angle range: approximately 180° (at 500→2500 μsec)

Pulse width range: 500→2500 μsec

No-load speed: 0.12±0.01 sec/60 (DC 4.8V) 0.1±0.01 sec/60 (DC 6V)

No-load current: 200±20mA (DC 4.8V) 220±20mA (DC 6V)

Stopping torque: 1.3±0.01kg·cm (DC 4.8V) 1.5±0.1kg·cm (DC 6V)

Stop current: ≦850mA (DC 4.8V) ≦1000mA (DC 6V)

Standby current: 3±1mA (DC 4.8V) 4±1mA (DC 6V)

1.3 Wiring Diagram

Expansion Board

Servo

GND

G(Brown)

3V3

V(Red)

P1 / io14

S(Yellow)

Servo

1.4 Code

Note: The rotation angle of the servo is 0-180 degrees, but if the crocodile has been assembled, the angle must be rotated between 55-130 degrees, otherwise the servo will get hot and burn out.

Code File:

Download link:Download

Download the code file and unzip it, then double-click microbit-1-Servo_Code1.hex to open the file and upload the code.

Add code manually:

1.Tap to add servo library.

2.Input “Servo” and click to add Servo library.

3.It is successfully added.

4.Drag into from ,and set the pin to P1 and Angle to 55 °.

5.Dragfrom and place it under the ,and set the delay to 1000ms.

6.Repeat steps 4 and 5 to add the code blocks to set the servo to 90 ° and 110 ° with a delay of 1000ms.

Complete Code:

1.5 Test Result

After uploading the code, the crocodile will open its mouth for 1s, then close half of its mouth for 1s, and then completely close its mouth.

1.6 Extended Tutorial

Earlier we have controlled the crocodile to open and close its mouth at a wide angle, so how to control the crocodile to slowly close and open its mouth?

1.6.1 Code

Code File:

Download link:Download

Download the code file and unzip it, then double-click microbit-1-Servo_Code2.hexto open the file and upload the code.

Add code manually:

1.Click in and add a variable named value.

2.Dragfrom into,and set the value to 55 in the white box.

3.Dragfrom intomb11,and set the repeat times in the white box to 55.

4.Dragfrom mb21intoimage-20240411140012957.

5.Dragimage-20240411134928930 and fromimage-20240411135053755 place it under theimage-20240411134815736,and set pin to P1.

image-20240411140622035

6.Dragimage-20240411140703032from mb21and place it in the in the white box of image-20240411140727409.

image-20240411140801321

7.Dragimage-20240411135225827from image-20240411135120617 and place it under the image-20240411140848252,and set the delay to 30ms.

image-20240411140905994

9.Right-clickimage-20240411140012957and tap Duplicate”.

image-20240411140946657

10.Add the copied code block below image-20240411141156778,then modify “1” in the white box of image-20240411134815736 to “-1”.

image-20240411141318351

Complete Code:

image-20240411133752416

1.6.2 Test Result

After uploading the code, the crocodile will slowly open its mouth and then slowly close it.

2.Ultrasonic Module

2.1 Introduction

The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like what bats do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package. It comes with an ultrasonic transmitter and a receiver.

It is being used in a wide range of electronics projects for creating obstacle detection and distance measuring application as well as various other applications.

2.2 Specification

Working voltage:3.3-5V

Quiescent current: <2mA

Working current: 15mA

Effective angle: <15°

Distance range: 2cm – 400 cm

Accuracy: 0.3 cm

Measuring angle: 30 degrees

Trigger input pulse width: 10 microseconds

wps3-1687242720835-4

2.3 Wiring Diagram

Expansion Board

Module

GND

G

3V3

V

P8 / io4

Trig

P12 / io15

Echo

ultrasonic

2.4 Code

Note: The measuring distance of ultrasonic is 2-300cm, but after being assembled on the crocodile, it can only detect 4-30cm. For the ultrasonic receives the bounced signal at a certain angle, but the basswood board of the crocodile body blocks it. As a result, only 30cm can be recognized, but this does not affect our hand biting crocodile tutorial.

Code File:

Download link:Download

Download the code file and unzip it, then double-click microbit-2-Ultrasonic_Code.hexto open the file and upload the code.

image-20240412082118573

Add code manually:

1.Tapimage-20240411141436376to add sonar library.

image-20240411131330336

2.Input “sonar”and clickimage-20240411131603613to add sonar library.

image-20240411151421395

3.The sonar library is added.

image-20240411151559812

4.Dragimage-20240411151657614from image-20240411151634760into mb24.

image-20240411151752157

5.Clickimage-20240411135444604 in mb21 and add a variable named value.

image-20240411135607139

6.Dragmb23from mb21intomb11.

image-20240411152209708

7.Dragimage-20240411152100056from image-20240411152033812 into the white box of mb23,and set trig to P8 pin, echo to P12 pin, unit to CM.image-20240411152339265

8.Dragimage-20240411152425981from image-20240411151634760 and place it under the image-20240411152456476,and modify the character in the first white box to “distance=”.

image-20240411152553210

9.Dragimage-20240411140703032from mb21and place it into the second white box ofimage-20240411152717631.

image-20240411152816974

10.Dragimage-20240411135225827from image-20240411135120617and place it under the image-20240411152957623,and modify the delay to 500ms.

Complete Code:

image-20240411155438811

2.5 Test Result

If you cannot print data in the browser’s makecode compilation interface, use CoolTerm software. Details are in the micro bit basic tutorial.

After uploading the code, open the CoolTerm software, click Options, select SerialPort, set the COM port and baud rate to 115200 (after testing, the USB serial communication baud rate of the micro:bit board is 115200), then click OK and Connect.

image-20240411161642015

After the setting is completed, we can see the distance sensed by the ultrasonic in the serial port printing area. The serial port printing distance will be printed every 0.5s.

image-20240411155421424

3.Buttons Control the Crocodile

3.1 Introduction

In this project, we work to control the crocodile to open and close its mouth through the AB buttons on the microbit.

3.2 Code

Code File:

Download link:Download

Download the code file and unzip it, then double-click microbit-3-Key_controlled_crocodile_Code .hex to open the file and upload the code.

image-20240412082159608

Add code manually:

1.Add servo library then dragimage-20240411134928930from image-20240411135053755and place it intomb24,and set the pin to P1 and the angle to 110°.

image-20240411163021847

2.Dragimage-20240411163118548from image-20240411135120617and place it intomb24,and set the dot matrix to displaymb64.

image-20240411163213565

3.Dragimage-20240411163257914from image-20240411163233958, then add the set servo to 55 ° code and the dot matrix displaysmb68code.

image-20240411163431960

4.Copyimage-20240411163257914and alter button to B,then set servo angle to 110°,dot matrix displaysmb64.

image-20240411163540280

Complete Code:

image-20240411162817429

3.3 Test Result

After the code is uploaded successfully, press the button A and the crocodile will display mb68and close its mouth. Press the button B and the crocodile will displaymb64and open its mouth.

4.Hand Biting Crocodile

4.1 Introduction

The crocodile opens its mouth. When you put your hand into the crocodile’s mouth, the ultrasonic on the crocodile will measure the depth of your hand into the crocodile’s mouth. When it reaches the depth we set, the crocodile will bite.

4.2 Code

Code File:

Download link:Download

Download the code file and unzip it, then double-click microbit-4-Crocodile_Bite_Code.hex to open the file and upload the code.

image-20240412082245262

Add code manually:

1.Add the servo library and the ultrasonic library, and generate variables “distance” and “value”.

2.Add image-20240416104740404intomb24.

image-20240411165204056

2.Addmb23intomb11,and set variable to ‘distance’.

image-20240411165357924

3.Addimage-20240411152100056intoimage-20240411165525534,and set trig to P8 pin, echo to P12 pin, unit to CM.

image-20240411165615989

4.Dragmb78from mb79and place it under the image-20240411165759723.

image-20240411165825260

5.Dragmb82from mb79and place it under the diamond box of mb78.

image-20240411170008163

6.Addimage-20240411170057223to the left box of mb82image-20240411170134627to the right box ofmb82;distance < value.

image-20240411170236525

7.Addmb23into image-20240411170420122.

image-20240411170539546

8.Dragimage-20240411170616999from image-20240411170603994intomb23,and set the value to 3 to 8.

image-20240411170718826

9.Add code inimage-20240411171045550,first, set the servo to 55°,dot matrix displays mb68,and delays 3s,then set the servo to 110°,dot matrix displaysmb64and delays 1s.

Complete Code:

image-20240411164726559

4.3 Test Result

After the code is successfully uploaded, the crocodile opens its mouth, and the dot matrix displaysmb64. When the distance between the hand and the ultrasonic meets the crocodile’s bite conditions, the crocodile bites and the dot matrix displays mb68. The crocodile releases its mouth after 3s of bite. and the dot matrix displaysmb64, the final 1s delay is to prepare for retracting the hand from the crocodile’s mouth. After 1s, it enters the next hand-biting process.