Introduction: Build a Robot Car With an 80s Style Home Computer As Controller.

About: Loves to play with things - 70s retro computing and microcontrollers is my interest at the moment.

On almost all electronics web shopping sites there are kits for two wheeled or four wheeled robots. The content of these kits are pretty much the same. Motors and wheels, a motor controller based on the LM298 chip and an Arduino UNO. They also come with a few sensors. The ultrasonic distance sensor is always included. Some have a remote control and a few other components. The frame is of transparent plexiglas. I bought mine at Banggood a while ago. When I started to assemble it, I found that this kit cannot be used to build a robot without additional components. You need to add things and build something individual. This Instructable is about my attempt to do so.

I wanted the car to be self driving and do autonomous things. I also wanted to use my IoTBASIC interpreter on it and control the activity of the car with a BASIC program. With this interpreter language, I can log on the robot, modify the program and restart it without needed the Arduino IDE or any other development system. Essentially this is as if an 80s style home computer would be put on wheels.

While this design can be build with the original components, the UNO and the motor control board supplied with the kit, I mainly write about a car with an Arduino R4 and the Arduino Motor Shield R3. The R4 has 32 kB of memory and a big flash. It can run the full BASIC interpreter and has 26 kB of usable memory for the BASIC program. The Arduino Motor Shield is much better quality than the motor board coming with the kit. It is easier to install and control. As an alternative to the Uno R4 an ESP32 Wemos R1 board can also be used. This is a powerful board at a low price. It is compatible with the Arduino Motor Shield.

Supplies

To build this robot, you first of all need the robot kit. I used a four wheel kit. They can be bought practically everywhere.

Additional components usually not in the kit are

  • An Arduino R4 Minima, R4 Wifi or ESP32 Wemos R1 board.
  • The Arduino Motor Shield R3.
  • A HC05 Bluetooth to serial controller to log on to the robot remotely.
  • A 32kB EEPROM chip (optional).
  • Some plywood boards, rubber bands and other little things found in most workshops.

If you don't have a R4, I also added solutions with just the UNO, a Mega or a ESP32 Wemos R1 board. All of them work. I also will write a few words about the original motor controller hardware.

As a firmware you will need the 2.0 version of my Arduino BASIC interpreter which can be found here: slviajero/tinybasic: A BASIC interpreter for Arduino, ESP, RP2040, STM32, Infineon XMC and POSIX with IoT and microcontroller features. (github.com)

Most things also work with the 1.x version but I don't fix bugs in it any more.

Step 1: Build the Car Chassis

The base car can be build pretty easily. Remove the protective foil from the plexiglas base plates. They are identical but they have a front and a rear which is different. Some people leave the foil on the plates but I found that it looks odd. In my kit I had to solder the wires to the motors. Then mount the yellow motor holders to the lower plate and insert the motors. After that the wheels can be inserted.

I then soldered the wires of the two motors together on the right and left side. Both left side motors are later connected to channel A of the motor controllers. The right side motors go to channel B.

If you build the car with the Arduino Motor Shield you can now add the top plate. All this is pretty straight forward and all the components in my kit did fit together well.

The top part of the kit was a little harder to build.

If you want use the standard motor electronics coming with the kit, there is some more later on.

Step 2: Build the Upper Part of the Robot.

The upper part of the robot is meant to have the ultrasonic distance sensor. There is a servo together with some plastic parts which could serve as a mount for the sensors. With this the robot can look around and measure the distance to obstacles in different directions.

These parts however did not fit together at all in the kit hat I had bought. The holes in the plexiglas to plate did not fit and the mount for the sensor also did not fit. The servo could not be inserted properly into the holder plate that came with it. Obviously someone had thrown together some parts that were never meant to fit together.

To build the robot I decided to mount the active components on a plywood plate that I would mount on to of the plexiglas plates coming with the kit. Plywood is pretty elastic. It can absorb friction much better than plexiglas which can develop fissures if strong forces are applied to it. The plywood hat would stabilise the car and components can be mounted easily on it. Cutting, drilling and gluing plywood pieces can be done with simple tools.

As a basis I use a 23 cm long plywood plate with a 6 cm nose at the front.Holes were drilled exactly at the points where the upper part of the car is screwed to the lower part. This way you only need one set of screws to put everything together. Also, cut out two circles for the motor cables.

On top of this base plate I add two wooden strips that a glued on the base plate and fixed with small screws from below. On the wood strips comes the battery pack close to the front wheels.

The Arduino with it's Motor Shield on top and a small breadboard are mounted on a small plywood plate. I usually glue the base of the Arduino and the breadboard to the wood with two sided adessive stickers or tape. I removed one of the power lines of the breadboard to make it a little narrower. With this modification the three components fit exactly between the two holes for the engine cables.

Mounting the Arduino and the breadboard on the second plywood plate makes it easy to remove it later and replace it with other components. Simple short metal screws can be used to hold the Arduino and the battery pack on the wood strips.

Lastly, the holder plate for the servo is screwed to the nose. The arms of the sensor didn't fit into it in my kit. I cut the arms to the right side and then screwed it to the holder plate.

At the rear of the base plate you can see a set of lustre terminals screwed from the bottom on the wood plate close to the hole for the engine cables. Putting the engine cables through the hole and then attaching it to the lustre terminals helps to keep the cabling clean on the top later on.

The holder for the sensor also didn't really fit in my kit. I improvised with plywood pieces and a rubber band to fix it in my first build.

With all this done, the upper part of the robot can be fixed and we can add the electronics.

Step 3: Adding the Motor Electronics

Let's start from the rear of the robot.

We have the Arduino R4 with the Arduino Motor Shield. The two channels A and B of the Motor Shield are connected to the motor cables with the lustre terminal. I reverse the polarity of the one motor set here. As the motor sets are mounted opposite to one another one runs backward and the other runs forward if you cable on with the same polarity.

In my design I power everything through the Motor Shield and connect the battery pack to the Vin terminals of the shield through a little female power plug. This way I can simply disconnect the electronics from power without fiddling with the batteries.

The Arduino Motor has two channels A and B. For each of the channels it uses four pins. One controls the direction, one the speed of the motor and one for the brake. An analog input pin can be used to measure the current. Pin the pin sets are:

  • Channel A: D12 direction, D3 speed, D9 brake, A0 current.
  • Channel B: D13 direction, D11 speed, D8 brake, A1 current.

All other pins of the Arduino are free.

We will need three more pins. Two for the trigger and echo signal of the sensor and one to control the servo. We also will need the TX and RX pins and the SCL and SDA pins later. Connect the power output and ground of the Arduino to the one remaining power line of the breadboard. Also, cable the sensor pins to the breadboard. The servo pins can be directly connected to the Arduino Motor Shield. This will be explained later. Currently we only deal with the motors.

Step 4: Install the Firmware and Test the Motor.

As a firmware for the robot I used the Arduino IoT BASIC interpreter version 2 from my repo slviajero/tinybasic: A BASIC interpreter for Arduino, ESP, RP2040, STM32, Infineon XMC and POSIX with IoT and microcontroller features. (github.com).

You will need the files from Basic2/IoTBasic to import as a project IoTBasic in the Arduino IDE.

The repo contains the interpreter IoTBasic.ino with the header basic.h, the runtime environment with its header file runtime.cpp and runtime.h and two more files. One is language.h and the other is hardware.h. You will need to edit these files to switch on certain features of the interpreter.

If you use an Arduino R4 board or an Arduino Mega, set the defintion #define BASICFULL close to the beginning of the language.h file.

#define BASICFULL
#undef BASICINTEGER
#undef BASICSIMPLE
#undef BASICMINIMAL
#undef BASICSIMPLEWITHFLOAT
#undef BASICTINYWITHFLOAT

Make sure that all other language versions are #undef and do not change any other setting in this file. For a first test, you do not need to change hardware.h. If you have an Arduino UNO, use BASICSIMPLE for now.

#undef BASICFULL
#undef BASICINTEGER
#define BASICSIMPLE
#undef BASICMINIMAL
#undef BASICSIMPLEWITHFLOAT
#undef BASICTINYWITHFLOAT

After setting the port and the correct board type, compile upload the BASIC interpreter like any other Arduino sketch.

After a successful upload, open the serial monitor. Speed should be set to 9600 baud and life feed option should be Newline only. BASIC should report with an import prompt.

The interpreter has full control over all Arduino I/O functions. We can test this by switching on the one motor set for a second and then switch it off again.

Type in the program

10 PINM 13, 1
20 PINM 8, 1
30 DWRITE 13, 1
40 AWRITE 11, 120
50 DELAY 1000
60 AWRITE 11, 0

Program download link: firsttest.bas

Type in LIST and check the output. Type SAVE to store the program in the EEPROM. Then type RUN. The motor attached to channel B should run for a second and then stop again.

Congratulations, you have a BASIC programmable robot now.

The first two lines of the program set the pin mode of the two digital pins involved to output. Line 30 sets the direction of the motor by putting the digital pin 13 to HIGH. Then we send a pulse wave to the speed pin. 120 is about 4V if you use a 9V battery pack. After a second the pulse wave is turned off again.

Programs can be stored to and reloaded from the EEPROM. If you power off your Arduino and power it on again later on, you can always reload your program typing LOAD.

The program above works on the UNO, the R4 and the Mega in the same way. Later on I will write a few words on the ESP32. There, you will need different pin settings

We have not used the break in this example. Its function will be explained in the next step.

Step 5: Why Use BASIC? and How to Use It.

Why would someone use a programming language from 60s and 70s in the 21st century? My love for this language has grown again after I started to play with Arduino kits. As it is an interpreter language, all commands can be entered and executed interactively. Programs can be modified interactively at well.

Rerun the program again typing RUN. Motors will start and stop.

Type

AWRITE 11, 150

on the command line. The motor will run at a low speed continuously. Any AWRITE with a different last parameter will make the motors run with a different speed.

Type in

DWRITE 8, 1 

now. The motors will stop. The break pin of the shield switches off the motors.

DWRITE 8, 0 

restarts them.

You can play around now on the command line make both motors turn in different directions with different speeds. 

If you are unfamiliar with the language, you might want to have a look in the manual. There is also a tutorial in my repo.

Step 6: Building the Robot With the LM298 Motor Driver Board

The Arduino Motor Shield and the little motor controllers in the kits use the same chip. It is an LM298 H bridge. There are a lot of excellent tutorials on this chip and the motor control board here on Instructables. Use the schematics in them if you want to use this hardware.

The main difference of the two boards is the way the H bridge is triggered. Like the Arduino Motor Shield, there are two input lines for the pulse wave controlling the speed. They are called EA and EB. The direction and brake logic differ. IN1 and IN2 control the one motor. IN3 and IN4 control the second motor. If you connect IN1 to D13, IN2 to D8 and EA to D11 you can use the same sketch as above with a standard H bridge.

Again, you can check this out on the command line. 

Step 7: Add an Ultrasonic Sensor

To use the sensor cable the trigger pin of the sensor to pin 4 of the Arduino and the echo pin to pin 5. Connect power to the sensor.

 Type NEW to delete any existing program. Then type in the program

10 PINM 4, 1
20 PINM 5, 0
30 DWRITE 4, 0
40 PULSE 4, 10 
50 E=PULSE(5, 1, 100)
60 PRINT E/29*100/2
70 DELAY 500
80 GOTO 40

Program download link: echo.bas

Run this program once with RUN. If the sensor is connected and working properly, the program will output the distance of the next object in front of the sensor in milli meters.

The PULSE command in line 40 sends a pulse of 10 time 10 microseconds to pin 4. The PULSE function in line 50 is the well known pulseIn() of the Arduino IDE. It measures the pulse length on pin 5, raising flank with a timeout of 100 times 10 microseconds.

The program runs in an infinite loop. Sending the character # will end the loop and the input prompt returns.

 A word on the units here. Unlike in the Arduino IDE, BASIC uses 10 microseconds as the smallest unit of the PULSE command. This is because the interpreter runs slower than C programs and processes one command per 4 microseconds on many platforms. For use cases that require faster reaction times, BASIC is probably not the right language. The base unit of PULSE is a parameter that can be set at runtime with the SET command. Please look at the manual for more on this. 

Step 8: Add the Servo

Servos are driven with one pin only. Connect your servo to power and ground and then connect the control pin to pin 6 on your Arduino. The plugs of the servos in the robot kits sometimes do not fit with the Arduino Motor shield. The shield has the control pin the middle between power and ground. Standard servos may have a different pin order. Some tinkering is required here. I simply rewired the plug on the servo.

To move the servo the Arduino sends a periodic pulse to this pin and the servo adjusts the angle according to the pulse length in small steps. There are many libraries to do this in C. Some of them are really sophisticated, use interrupts and do all the servo control work in the background. Für this project here I decided to keep things simple and to all the servo control in BASIC.

 Delete your old program with NEW and type in

10 PINM 6,1 
20 DWRITE 6, 0
30 INPUT A
40 P=40+A*200/180
50 FOR I=1 TO 40: DELAY 20: PULSE 6, P: NEXT
60 GOTO 30

Program download link: servo.bas

Then RUN the program. It will ask you to input an angle A. It then calculates the pulse width P and sends 40 pulses in 20 millisecond intervals. The servo will move to the respective angle. This is a crude way of doing it but it works. With just one servo to control, the code above is all we will need.

To interrupt the program, type in # as the first character of the input. Sending # will also interrupt running programs.

You can play around with the parameters in the equation in line 40. The first one is the zero point and the second one maps the angle to the right pulse length. Some tweaking may be needed.

With this step we have all components of the car itself completed. 

Step 9: Remote Control With the HC05 Bluetooth Module

So far the whole system is still attached to the USB connector of the computer. To make it a little more independent, we need some radio connectivity. The easiest and cheapest way of doing it is a classical Bluetooth connection.

There are many excellent tutorials on the HC05. Please look at them how to pair your computer and set them up. I recommend

This work uses SoftwareSerial and pins 10 and 11 to connect the module. We do not need that here and connect the module directly to the UART of the Arduino.

If you built the robot with an Arduino UNO or a MEGA, you simply can connect the HC05 TX line with the Arduino RZ line and vice versa.

Disconnect the Arduino from the power line, connect the battery and then put it on the floor.

Open the Arduino IDE again, set the port on your computer to the paired Bluetooth port of the HC05 and then open the serial monitor.

You should again see the BASIC input prompt again. You can now enter commands and remote control the robot from your computer.

For the Arduino R4 things are a bit more complicated. As the external serial port on the board is accessed through the object Serial1, the firmware has to be changed.

In your Arduino IDE, open the hardware.h file and add the line

#define ALTSERIAL Serial1

 and then upload the BASIC interpreter again. BASIC now uses Serial1 and the command prompt should appear in the serial monitor after connecting and pairing the HC05.

Step 10: Run the Turtle

Now it is time to run a real program on the robot. I called it the turtle because it somehow reminded me of the turtle graphics of the LOGO programming language. It is a simple remote control program. Commands are send to the robot as strings and then executed. The robot knows 4 commands and can process one argument from 0-9. These commands are

  • f: drive forward
  • b: drive backward
  • l: turn left
  • r: turn right
  • 0-9: set the runtime of the motors to 100+x*100 milliseconds

A BASIC program to do this would look like this

10 REM "engine code, the pins of the Arduino motor shield"
20 DA=12: SA=3: BA=9
30 DB=13: SB=11: BB=8
40 TP=4: EP=5
100 REM "set the pins, Dx is direction, Bx is break"
110 PINM DA,1: PINM BA,1
120 PINM DB,1: PINM BB,1
130 AWRITE SA,0: AWRITE SB,0
140 PINM TP, 1: PINM EP, 0
200 REM "some parameters, TT is step time, S is speed ie voltage"
210 TT=5
220 S=140
1000 REM "Test code"
1010 INPUT A$
1020 GOSUB 8000
1030 GOTO 1000
8000 REM "The turtle code: A$ has a set of commands"
8010 REM "Commands f,b,r,l and single digits for the distance"
8020 REM "Prepare, set the speed and directions and hit the break"
8030 DWRITE BA,1: DWRITE BB,1
8040 AWRITE SA,S: AWRITE SB,S
8050 DWRITE DA,0: DWRITE DB,0
8100 REM "every character in A$ is a command"
8110 WHILE LEN(A$)>0
8120 C=A$: A$=A$(2)
8130 IF C=>"0" AND C<="9" THEN TT=C-"0": CONT
8140 SWITCH C
8150 CASE "e": END
8160 CASE "f": DWRITE DA,0: DWRITE DB,0: GOSUB 9000
8170 CASE "b": DWRITE DA,1: DWRITE DB,1: GOSUB 9000
8180 CASE "l": DWRITE DA,0: DWRITE DB,1: GOSUB 9000
8190 CASE "r": DWRITE DA,1: DWRITE DB,0: GOSUB 9000
8200 SWEND
8400 REM "Report the distance after each command"
8410 DWRITE TP,0: PULSE TP,10
8420 E=PULSE(EP,1,100)/29*100/2
8430 PRINT "Distance:",E
8500 WEND
8900 RETURN
9000 REM "drive a bit, TT says how long"
9010 DWRITE BA,0: DWRITE BB,0
9020 DELAY 100+TT*100
9030 DWRITE BA,1: DWRITE BB,1
9040 RETURN

It can be downloaded as turtle.bas from my repo.

This program is rather long and it would be tedious to type it in. You probably want to get a terminal program with a file upload feature at this point. I recommend Roger Meier's Coolterm from http://freeware.the-meiers.org. With this you can upload the file with the "Send File Command".

The program uses the structured language feature set of BASIC. It will run on the R4 and the MEGA but not on an UNO.

After uploading or typing the program, type RUN to start it. It will ask you for input. Type a few of the command letters and play with it right now. Every time a command has been processed, the distance is measured and reported.

The SAVE command will save the program to your EEPROM. LOAD reloads it.

Step 11: Add External File Storage

As BASIC programs for the robot become bigger, the internal EEPROM of the Arduino might not be enough. Also, the internal EEPROM can only hold one program at a time. To store multiple programs on the Arduino, an external mass storage is needed.

BASIC supports multiple options for this. For a robot, an external EEPROM is the best option. It can be plugged in and out, even while the robot is powered on and can store BASIC programs and data. I use 32kB EEPROM modules. They are cheap, available everywhere and can be used with an I2C connection. This keeps hardware complexity at a minimum.

Put the module on the breadboard and connected it to power, ground and the two I2C signals SDA and SCL.

To integrate the mass storage, an additional setting in the firmware is needed. Connect your robot to the computer, open the Arduino IDE and look for the ARDUINOEFS macro. Set

 #define ARDUINOEFS

EEPROM modules have I2C addresses between 0x50 and 0x57. Default in BASIC is 0x50. If your EEPROM has a different address, look for the macro EFSEEPROMADDR and set the address correctly.

Once you have compiled and uploaded the firmware, go to the BASIC command line prompt.

The FDISK command formats the internal disk. It requires one argument. This is the number of slots the EEPROM is divided into. EFS is a very simply file storage. It has fixed length slots. Formating a 32kB EEPROM with

FDISK 4

 would create 4 slots of 8 kB for programs. Saving and loading a program is done with the SAVE and LOAD command with a file name argument. File names can be 12 characters long.

Programs stored in the internal EEPROM can still be accessed with the commands LOAD "!" and SAVE "!".

Step 12: Running the Robot With ESP32

The Arduino R4 is a great board to build BASIC programmable robots. There is, however, another board I would like to mention here. A while a go WEMOS made an ESP32 board with an Arduino UNO form factor. It is still on the market and can be bought for under 10 Euros in various shopping sites.

The board is a 3.3V board and compatibility with Arduino shields is not guaranteed. Moreover the pinout is completely different from the Arduino UNO and no attempts where made to ensure compatibility in the board definition files. That's why the board never became very popular.

To use it on your robot, you need to upload BASIC with slightly different hardware settings. Open hardware.h, look for the hardware definition section and set the following definitions:

#define ARDUINOEEPROM
#define ESPSPIFFS
#define ARDUINOWIRE

All the other settings should be #undef. If you have set ARDUINOEFS and ALTSERIAL for earlier projects, please set them again to #undef.

The board needs the ESP32 board files in Arduino board manager. Choose the WEMOS D1 R32 board definition to compile the interpreter. Also, before uploading, disconnect the bluetooth module. Then upload the interpreter and reconnect the bluetooth module.

All the electronics are identical except for the external EEPROM which can be removed. If you connect to the bluetooth module again you can enter commands just like before.

With the ESP32 the BASIC interpreter has 64kB of memory and there is a 1 MB internal flash disk for program storage. Enter FDISK to format the internal flash if you use the board for the first time.

You can use all the test programs as before but you need to map the pins. The first few lines of the turtle program woudl read

10 REM "engine code, the pins of the Arduino motor shield"
20 DA=19: SA=25: BA=13
30 DB=18: SB=23: BB=12
40 TP=17: EP=16

Everything else is identical. The full program with this pin settings can also be downloaded: turtleesp32.bas

Using the Arduino Motor Shield on this board could cause some raised eyebrows. This shield is 5V. The board is 3.3V. Any output by the shield could damage the board. There is a risk that the input from the current measurement on A0 and A1 damage the board but I have never had any trouble.

The parameter S in line 220 controls the PWM voltage. You can play with this parameter if the robot moves too slow.

External EEPROMs are compatible between board with BASIC. If you still want to use it #undef ESPSPIFF and use ARDUINOEFS instead. Then the EEPROM works as before.

Step 13: Run MQTT and Control With Messages

The big advantage of the ESP32 board is the network support. BASIC integrates Wifi and MQTT support. If you want to use it, you need to edit the file wifisettings.h

const char* ssid = "myssid";
const char* password = "mypasswd";
const char* mqtt_server = "test.mosquitto.org";
const short mqtt_port = 1883;

and enter your Wifi SSID and password. If you want to use an own MQTT server, add it here. Currently only unencrypted and unauthenticated MQTT is supported. The settings are compiled in the code and cannot be changed or accessed by BASIC at runtime.

Before you compile, download the standard Arduino PubSubclient (https://pubsubclient.knolleary.net) library. It is integrated in my BASIC interpreter.

In hardware.h set

#define ARDUINOMQTT

recompile the code and upload it again. Don't forget to disconnect the bluetooth module before upload and then reconnect it again.

When you reconnect the to the robot. Type

> NETSTAT 

you should the the status of your network connection. It should be up an MQTT should be disconnected state 255.

Network connected 
MQTT state 255
MQTT out topic 
MQTT inp topic 
MQTT name iotbasicxxx

Once connected to the network, the BASIC interpreter can handle MQTT message through a special input and output channel. A modified turtle program uses this to control the robot.

10 REM "engine code, the pins of the Arduino motor shield"
20 DA=19: SA=25: BA=13
30 DB=18: SB=23: BB=12
40 TP=17: EP=16
100 REM "set the pins, Dx is direction, Bx is break"
110 PINM DA,1: PINM BA,1
120 PINM DB,1: PINM BB,1
130 AWRITE SA,0: AWRITE SB,0
140 PINM TP, 1: PINM EP, 0
200 REM "some parameters, TT is step time, S is speed ie voltage"
210 TT=5
220 S=140
300 REM "Open MQTT channel"
310 OPEN &9, "littlerobot/data", 1
320 OPEN &9, "littlerobot/command"
1000 REM "MQTT code"
1010 INPUT &9, A$
1020 IF A$<>"" THEN GOSUB 8000 ELSE DELAY 500
1030 GOTO 1000
8000 REM "The turtle code: A$ has a set of commands"
8010 REM "Commands f,b,r,l and single digits for the distance"
8020 REM "Prepare, set the speed and directions and hit the break"
8030 DWRITE BA,1: DWRITE BB,1
8040 AWRITE SA,S: AWRITE SB,S
8050 DWRITE DA,0: DWRITE DB,0
8100 REM "every character in A$ is a command"
8110 WHILE LEN(A$)>0
8120 C=A$: A$=A$(2)
8130 IF C=>"0" AND C<="9" THEN TT=C-"0": CONT
8140 SWITCH C
8150 CASE "e": END
8160 CASE "f": DWRITE DA,0: DWRITE DB,0: GOSUB 9000
8170 CASE "b": DWRITE DA,1: DWRITE DB,1: GOSUB 9000
8180 CASE "l": DWRITE DA,0: DWRITE DB,1: GOSUB 9000
8190 CASE "r": DWRITE DA,1: DWRITE DB,0: GOSUB 9000
8200 SWEND
8400 REM "Report the distance after each command"
8410 DWRITE TP,0: PULSE TP,10
8420 E=PULSE(EP,1,100)/29*100/2
8430 PRINT &9, "Distance:",E
8500 WEND
8900 RETURN
9000 REM "drive a bit, TT says how long"
9010 DWRITE BA,0: DWRITE BB,0
9020 DELAY 100+TT*100
9030 DWRITE BA,1: DWRITE BB,1
9040 RETURN

The main changes are lines 300 to 320. An MQTT topic for commands and data are opened. Best use your own name here. INPUT and PRINT commands use &9 to indicate that the MQTT channel is to be used. Download the program from turtlemqtt32.bas.

Upload and run the program.

To send MQTT messages you can use any MQTT client. On my iPhone I use EasyMQTT (https://www.easymqtt.app) because the free version offers already all I need.

Send commands for the robot to the littlerobot/command topic and observe the answers in the littlerobot/data topic.

A running program can be stopped by sending # on the serial line. If you type NETSTAT again after the program has run, you will see an output like this

Network connected 
MQTT state 0
MQTT out topic littlerobot/data
MQTT inp topic littlerobot/command
MQTT name iotbasicD4T

It shows the status of the MQTT connection, the name of input and output topics and the randomly generated name of the robot on the MQTT broker.

If you have a Arduino R4 Wifi board, all this works just as well. Only the PIN settings in the BASIC program need to be set to the standard values at the beginning of the BASIC program.

10 REM "engine code, the pins of the Arduino motor shield"
20 DA=12: SA=3: BA=9
30 DB=13: SB=11: BB=8
40 TP=4: EP=5

Step 14: Add an MQTT Remote Control

Now that the turtle understands MQTT messages, it is quite easy to add a remote control.

I use and Arduino R4 Wifi for it. The R4 Wifi is a 5V board and compatible with the standard LCD Shield. Alternatively a Wemos D1 8266 board can also be used for this. They are cheaper than the R4 Wifi and compatible with the LCD library. I could not get the ESP32 board to run with the LCD shield. Like all the Arduino lookalike shield, there are pinout / pin use conflicts.

The hardware settings for the BASIC interpreter need to be

#define DISPLAYCANSCROLL
#define LCDSHIELD
#define ARDUINOEEPROM
#define ARDUINOMQTT

all other definitions have to be #undef in hardware.h.

Mount the LCD Shield on the board. Compile and upload the BASIC interpreter on the Arduino Wifi.

Then upload or type in the BASIC program

10 OPEN &9,"littlerobot/data"
20 OPEN &9,"littlerobot/command",1
30 S=0
40 WHILE -1
50 GET &2,C
60 SWITCH C
70 CASE "1": PRINT &9,"l"
80 CASE "2": PRINT &9,"r"
90 CASE "3": PRINT &9,"b"
100 CASE "4": PRINT &9,"f"
110 SWEND
120 IF AVAIL(9)=0 THEN CONT
130 INPUT &9,A$: IF A$<>"" THEN CLS: PRINT &2,INT(VAL(A$))
140 WEND

Program download link: remote.bas

SAVE and RUN it. Pressing the buttons on the shield will send the turtle commands f, b, r, l to the MQTT topic littlerobot/command and display the answer from the topic littlerobot/data.

If you now run the MQTT turtle program on your robot, you can see it move.

Step 15: What's Next?

The way the turtle is controlled very much resembles the Mars robots of NASA. Messages are sent and then the robot does a small step and reports back data. MQTT is not a real time control protocol, after all. One could now add more logic to the turtle and make it more autonomous. In the end it should be able to travel and explore alone and report back images. This will be my next project.


Step 16: This and That - Technical Resources

Pin Map ESP32 Wemos D1 and Arduino UNO/R4/MEGA

Arduino / Wemos D1
13 / 18
12 / 19
11 / 23
10 / 5
9 / 13
8 / 12
7 / 14
6 / 27
5 / 16
4 / 17
3 / 25
2 / 26
1 / 1
0 / 3

Source: https://community.platformio.org/t/wemos-d1-r32-any-users/15304/4

Pin Map ESP82866 and Arduino

static const uint8_t D0   = 3;
static const uint8_t D1  = 1;
static const uint8_t D2  = 16;
static const uint8_t D3  = 5;
static const uint8_t D4  = 4;
static const uint8_t D5  = 14;
static const uint8_t D6  = 12;
static const uint8_t D7  = 13;
static const uint8_t D8  = 0;
static const uint8_t D9  = 2;
static const uint8_t D10  = 15;
static const uint8_t D11  = 13;
static const uint8_t D12  = 12;
static const uint8_t D13  = 14;
static const uint8_t D14  = 4;
static const uint8_t D15  = 5;

Source: https://forum.arduino.cc/t/wemos-d1-pins/523831/17

Other helpful things

A cool project from the Robotics Contest: Motor Drive Shield for Arduino NANO : 5 Steps - Instructables

With an Nano or better a Nano Every all the things I have done here would work with this board.

Also from the Robotics Contest: How to Make Simple Nodemcu Robotic Car : 7 Steps - Instructables

Could be used to run all these things on a NodeMCU.

And last but not least, learning obstacle avoidance: Lily∞Bot With Raspberry PI Pico W: Obstacle Avoidance With 3.3V Sonar Sensor : 3 Steps - Instructables

Robotics Contest

Runner Up in the
Robotics Contest