## Introduction: [2021] Servo Winch Challenge! Two (x2) Micro:bits & RC Car for Conquering Gravity

A winch is a mechanical device used to wind the rope, and it is made of a spool attached to a crank. How can it be used and is it useful? Some examples of using winches are, elevators, wrecker trucks and sailing boats. In the field of robotics and off-road vehicle competitions, winches are often used for towing things.

Can we replicate a winch mechanism in electronics DIY? By using a continuous servo, we can powerfully wind the rope with the spool. Unlike ordinary servos with the internal potentiometer to control the rotating angle of its shaft, continuous servos can rotate 360 degrees, allowing the attached spool to wind the rope powerfully.

Can we code for a winch mechanism? If we use Microsoft MakeCode editor, we can use Servos extension blocks for easily maneuvering a continuous servo and we can replicate a winch!

We will present how to set up two (x2) micro:bits, one as the transmitter and another one as the receiver on the car. These micro:bits are paired and you can control the car with the winch! We will use Valenta micro:bit RC car and its winch mod parts.

In this tutorial, you can download ready-to-play MakeCode sample files and play immediately. Controlling the car is made simple. Tilting your "transmitter" micro:bit down, up, right and left will let the car go forward, backward, right and left. On your "transmitter" micro:bit, you could calibrate the steering of the car by pressing button A or B. You can wind the string rope of the winch by pressing button A and B together.

This tutorial does not require coding at all. However, what's really fun is to reverse-engineer how it's running under the hood. If you want to discover more, you can further continue reading the following steps to go deeper in the knowledge. Good luck and have fun!

## Supplies

Micro:bits

We will need two (x2) micro:bits. One (x1) micro:bit is used for the transmitter. Another (x1) micro:bit is used for the receiver. We've tested and both micro:bit version 1 (V1) and version 2 (V2) are working correctly.

Batteries

We recommend using new, single-use four (x4) 1.5V AA batteries for the car.

Car and Winch

We will need one (x1) Valenta off-roader micro:bit RC car. It is Lego technic compatible and equipped with two (x2) micro gear motors on the rear wheels and one (x1) steering servo based on Roberval balance arm mechanism.

You can also refer to the instruction for assembling the car.

## Step 1: Copying the Sample File to Micro:Bit

You can download MakeCode sample files (below) to your computer. These sample files are ready to play, and you can start playing immediately. These sample files can work with both micro:bit version 1 (V1) and version 2 (V2). Connect your computer and a micro:bit via USB cable, and copy each file to each of your micro:bit one at a time.

First, drag and drop Transmitter.hex file to a micro:bit and use this as "transmitter" micro:bit.

Second, drag and drop Receiver.hex file to another micro:bit and use this as "receiver" micro:bit.

Once you've copied the sample files, disconnect micro:bits from your computer. Connect the battery case to "transmitter" micro:bit and turn on. Mount "receiver" micro:bit on your car and turn on the power switch on the motor controller. Once "transmitter" micro:bit and "receiver" micro:bit are turned on properly, they will start communicating and you can control the car. Tilting your "transmitter" micro:bit down, up, right and left will let the car go forward, backward, right and left.

By default, the steering is not always straight by the servo equipped in the car. If the steering is not straight ahead, try to calibrate it by pressing "transmitter" micro:bit's button A or B. If the steering is a little too right, press button A several times to fix it to the left. If the steering is a little too left, press button B several times to fix it to the right. Pressing either button will fix the steering angle by 2 degrees to the opposite side.

By pressing "transmitter" micro:bit's button A and B together, you can wind, stop, unwind and stop the string rope of the winch. Let's try climbing uphill using the winch!

## Step 2: Installing the Winch to the Car

We will install the winch to Valenta micro:bit RC car.

The car is equipped with micro:bit based motor controller called Valenta Zero. It contains a small motor controller (DRV8833) for controlling two (x2) DC motors on the rear wheels. There are four (x4) input and output (I/O) pins P0, P1, P2 and P8 for connecting devices like servos. Each of P0, P1, P2, P8 pins have three pins, Signal (Yellow), Voltage (Red) and Ground (Black). Please check if they are connected correctly. These pins can operate at 3.3V or 5V (selectable) using the jumper switch, and please choose 5V for connecting the servo.

The winch is made of the continuous servo for winding the string rope. Let's use P1 pin for connecting the continuous servo in this tutorial. (The sample file uses P1 for connecting the continuous servo.) Use the pins and holes for interlocking the parts and mount the winch on the front of the car.

## Step 3: Reviewing Transmitter.hex Sample File

Now we will examine Transmitter.hex sample file. Open MakeCode editor and click Import button. Open Transmitter.hex file you've copied to "transmitter" micro:bit.

on start block

This block is called initially at once when "transmitter" micro:bit is turned on. In Radio extension, you can find the block radio set group and 1 is set for example. This number must be the same between "transmitter" micro:bit and "receiver" micro:bit, so they can be paired together for communication.

We are using Accelerometer micro:bit features. By tilting your "transmitter" micro:bit down, up, right or left, it will send the radio string of "goForward" "goBackward" "goRight" or "goLeft" to your "receiver" micro:bit.

on logo down block

In Input extension, you can find the block on logo down. This block is executed whenever you tilt "transmitter" micro:bit down or forward. In the block, you can also find radio send string "goForward" that will send the radio string "goForward" over the air when "transmitter" micro:bit is tilted down. When "receiver" micro:bit receives this string, the car will go forward.

on logo up block

In Input extension, you can find the block on logo up. This block is executed whenever you tilt "transmitter" micro:bit up or backward. In the block, you can also find radio send string "goBackward" that will send the radio string "goBackward" over the air when "transmitter" micro:bit is tilted up. When "receiver" micro:bit receives this string, the car will go backward.

on tilt left block

In Input extension, you can find the block on tilt left. This block is executed whenever you tilt "transmitter" micro:bit to the left. In the block, you can also find radio send string "goLeft" that will send the radio string "goLeft" over the air when "transmitter" micro:bit is tilted to the left. When "receiver" micro:bit receives this string, the car will turn left.

on tilt right block

In Input extension, you can find the block on tilt right. This block is executed whenever you tilt "transmitter" micro:bit to the right. In the block, you can also find radio send string "goRight" that will send the radio string "goRight" over the air when "transmitter" micro:bit is tilted to the right. When "receiver" micro:bit receives this string, the car will turn right.

By default, the steering of your car is not always straight by the servo equipped in the car. If the car's steering is not straight ahead, you could calibrate it by pressing "transmitter" micro:bit's button A or B.

on button A pressed block

If the car's steering is a little too right, you can press button A several times to fix it to the left. In the block, you can also find radio send string "calibrateLeft" that will send the radio string "calibrateLeft" over the air when "transmitter" micro:bit's button A is pressed. When "receiver" micro:bit receives this string, the car will fix the steering angle by 2 degrees to the left.

on button B pressed block

If the car's steering is a little too left, you can press button B several times to fix it to the right. In the block, you can also find radio send string "calibrateRight" that will send the radio string "calibrateRight" over the air when "transmitter" micro:bit's button B is pressed. When "receiver" micro:bit receives this string, the car will fix the steering angle by 2 degrees to the right.

Pressing button A and B simultaneously will "wind" the string rope of the winch. Pressing them together again will stop winding. Pressing them together again will "unwind" the string rope in the opposite direction. Pressing them together again will stop unwinding.

on button A+B pressed block

In the block, you can find radio send string "wind" that will send the radio string "wind" over the air when "transmitter" micro:bit's button A and B are pressed simultaneously. When "receiver" micro:bit receives this string, the winch will wind, stop, unwind and stop.

## Step 4: Adding Extensions to MakeCode Editor

Does your MakeCode editor include Servos and Neopixel extensions? Please open the editor and check if it includes these extensions in the left menu. We will use these extensions in Receiver.hex sample file. If you can't find these extensions, click Extensions in the bottom of the menu. Then, click to add these extensions to the menu one by one.

Servos extension will be used for calibrating the steering angle.

Neopixel extension will be used for lighting up the programmable RGB LEDs built on the motor controller.

## Step 5: Reviewing Receiver.hex Sample File (Part 1)

Now let's look at Receiver.hex sample file. Open MakeCode editor and click Import button. Open Receiver.hex file you've copied to "receiver" micro:bit.

on start block

This block is called initially at once when "receiver" micro:bit is turned on.

In Radio extension, you can find the block radio set group and 1 is set for example. This number must be the same between "transmitter" micro:bit and "receiver" micro:bit, so they can be paired together for communication.

In this project, let's assume that the steering servo is attached to P2 pin on the motor controller. Let's configure that the servo swings from 0 to 180 degrees and its central angle is 90 degrees.

set center to 90

In Variable extension, create a new variable center that keeps the servo's central angle you wish. Drag and drop set centerto block and set it to 90 degrees.

set servo P2 range from 0 to 180

In Servos extension, drag and drop set servo P2 range from 0 to 180. Make sure to choose P2 pin and the rotation angle ranges from 0 to 180 degrees.

set servo P2 angle to center

In Servos extension, drag and drop set servo P2 angle to center. Make sure to set the angle to the variable center.

set LED RGB to NeoPixel at pin P16 with 4 leds as RGB

In Neopixel extension, drag and drop set strip to NeoPixel at pin P0 with 24 leds as RGB. Click the variable strip and rename it to LED RGB. The motor controller uses pin P16 for its four (x4) RGB LEDs onboard. Thus, choose the pin P16 and enter as 4 leds. Now the block looks like set LED RGB to NeoPixel at pin P16 with 4 leds as RGB.

set winch to 0

In Variable extension, create a new variable winch that keeps the boolean value of 0 or 1 for powering off and on the winch. Let's set winch to 0 initially.

set UP to 0

In Variable extension, create a new variable UP that keeps the boolean value of 0 or 1 for winding down and up the string rope. Let's set UP to 0 initially.

(Continuing to the next step)

## Step 6: Reviewing Receiver.hex Sample File (Part 2)

Let's look at the following function blocks that define the direction and the speed of your car.

The car has a micro gear motor M1 on the left rear wheel and M2 on the right rear wheel.

function goForward block

First, let's create goForward function in Functions extension.

digital write pin P13 to 0

P13 pin is used for direction of M1 motor. In Pins extension, drag and drop digital write pin P13 to 0, so that M1 goes forward.

digital write pin P15 to 0

P15 pin is used for direction of M2 motor. In Pins extension, drag and drop digital write pin P15 to 0, so that M2 goes forward.

analog write pin P12 to 1023

P12 pin is used for speed (max speed is 1023) of M1 motor. In Pins extension, drag and drop analog write pin P12 to 1023, so that M1 goes forward at max speed.

analog write pin P14 to 1023

P14 pin is used for speed (max speed is 1023) of M2 motor. In Pins extension, drag and drop analog write pin P14 to 1023, so that M2 goes forward at max speed.

pause (ms) 1000

In Basic extension, drag and drop pause (ms) 1000 block to keep the car going forward for 1000 milliseconds (1 second).

call stop

In Functions extension, drag and drop call stop block to execute function stop. After the car has run for 1 second, it will stop safely.

function goBackward block

Second, let's create goBackward function in Functions extension.

digital write pin P12 to 0

P12 pin is used for direction of M1 motor. In Pins extension, drag and drop digital write pin P12 to 0, so that M1 goes backward.

digital write pin P14 to 0

P14 pin is used for direction of M2 motor. In Pins extension, drag and drop digital write pin P14 to 0, so that M2 goes backward.

analog write pin P13 to 1023

P13 pin is used for speed (max speed is 1023) of M1 motor. In Pins extension, drag and drop analog write pin P13 to 1023, so that M1 goes backward at max speed.

analog write pin P15 to 1023

P15 pin is used for speed (max speed is 1023) of M2 motor. In Pins extension, drag and drop analog write pin P15 to 1023, so that M2 goes backward at max speed.

pause (ms) 1000

In Basic extension, drag and drop pause (ms) 1000 block to keep the car going forward for 1000 milliseconds (1 second).

call stop
In Functions extension, drag and drop call stop block to execute function stop. After the car has run for 1 second, it will stop safely.

function stop block

Lastly, let's create stop function in Functions extension.

digital write pin P13 to 0

P13 pin is used for direction of M1 motor. In Pins extension, drag and drop digital write pin P13 to 0, so that M1 is set for forward direction.

digital write pin P15 to 0

P15 pin is used for direction of M2 motor. In Pins extension, drag and drop digital write pin P15 to 0, so that M2 is set for forward direction.

analog write pin P12 to 0

P12 pin is used for speed (0 means no speed) of M1 motor. In Pins extension, drag and drop analog write pin P12 to 0, so that M1 stops.

analog write pin P14 to 0

P14 pin is used for speed (0 means no speed) of M2 motor. In Pins extension, drag and drop analog write pin P14 to 0, so that M2 stops.

set servo P2 angle to center

In Servos extension, drag and drop set servo P2 angle to center. Make sure to set the angle to the variable center. That way, the car's steering is always straight ahead every time it stops.

(Continuing to the next step)

## Step 7: Reviewing Receiver.hex Sample File (Part 3)

Previously, we've created the variable winch in Variable extension. This variable keeps the boolean value of 0 or 1 for powering off and on the winch.

We are using if then block from Logic extension. Click + sign to expand it as necessary. This block will sort out the winch status on and off using the boolean variable winch.

We've also created the variable UP in Variable extension. This variable keeps the boolean value of 0 or 1 for winding down and up the string rope.

We are also using "nested" if then block from Logic extension. Click + sign to expand it as necessary. This block will sort out winding status up and down using the boolean variable UP.

function wind block

First, let's create wind function in Functions extension.

if winch = 0 then

set winch to 1

LED RGB show color white

Let's see if winch is 0 (meaning the winch is off), thenset winch to 1 and turn on the winch. In Neopixel extension, drag and drop strip show color red. Swap the variable from strip to LED RGB that you've created earlier and choose the color of your choice, so that the block looks like LED RGB show color white. You can choose blue, yellow or any color you like.

if UP = 0

set UP to 1

continuous servo P1 run at 50%

else if UP = 1

set UP = 0

continuous servo P1 run at -50%

In the nested block, let's see if UP = 0 (meaning the winch is unwinding), then set UP to 1 and wind the string rope. In Servos extension, drag and drop continuous servo P1 run at 50%. If you want to wind faster, change it toward 100%. Make sure to plug the servo winch to the motor controller's P1 pins.

Let's see else if UP = 1 (meaning the winch is winding), then set UP to 0 and unwind the string rope. In Servos extension, drag and drop continuous servo P1 run at -50%. If you want to unwind faster, change it toward -100%. Make sure to plug the servo winch to the motor controller's P1 pins.

if winch = 1 then

set winch to 0

LED RGB show color black

stop servo P1

Let's see if winch is 1 (meaning the winch is on), then set winch to 0 and turn off the winch. In Neopixel extension, drag and drop strip show color red. Swap the variable from strip to LED RGB that you've created earlier and choose the color black, so that the block looks like LED RGB show color black. The black color means LEDs will turn off the light. In Servos extension, drag and drop stop servo P1, so that the winch is stopped.

(Continuing to the next step)

## Step 8: Reviewing Receiver.hex Sample File (Part 4)

Whenever "receiver" micro:bit catches the radio string sent from "transmitter" micro:bit over the air, "receiver" micro:bit will sort out and call the relevant function.

In Radio extension, find on radio received receivedString block and use it for sorting out and call the function. In Logic extension, use if then block and click + sign to expand it as necessary. This block will call the function depending on the received string being sorted out.

set center to center + 2

set servo P2 angle to center

If the received string is "calibrateLeft" then drag and drop set center to block in Variable extension here and make it set center to center + 2. It means you could fix the steering to the left by 2 degrees. In Servos extension, drag and drop set servo P2 angle to center. This center has been updated to the left by 2 degrees.

set center to center - 2

set servo P2 angle to center

If the received string is "calibrateRight" then drag and drop set center to block in Variable extension here and make it set center to center - 2. It means you could fix the steering to the right by 2 degrees. In Servos extension, drag and drop set servo P2 angle to center. This center has been updated to the right by 2 degrees.

else if receivedString = goForward then

call goForward

If the received string is "goForward" then drag and drop call goForward block in Functions extension to call the function.

else if receivedString = goBackward then

call goBackward

If the received string is "goBackward" then drag and drop call goBackward block in Functions extension to call the function.

else if receivedString = goLeft then

set servo P2 angle to center + 10

call goForward

If the received string is "goLeft" then, drag and drop set servo P2 angle to center + 10 in Servos extension. Use Math extension to create center + 10 bracket. It means the steering is shifted +10 degrees to the left. Drag and drop call goForward block in Functions extension to call the function. Now the car is turning left and going forward.

else if receivedString = goRight then

set servo P2 angle to center - 10

call goForward

If the received string is "goRight" then, drag and drop set servo P2 angle to center - 10 in Servos extension. Use Math extension to create center - 10 bracket. It means the steering is shifted -10 degrees to the right. Drag and drop call goForward block in Functions extension to call the function. Now the car is turning right and going forward.

else if receivedString = wind then

call wind

If the received string is "wind" then drag and drop call wind block in Functions extension to call the function.

That's all! You can also customize the sample file to your own way. Have fun!

Participated in the
Microcontroller Contest