Serial Communications

Introduction:

It is possible to communicate serially with the mBot once a program has been updated to the board. Serial communication can be via USB, 2.4GHz or Bluetooth. To communicate serially, you need to select the “Communication” extension from the Extensions menu:

comms-menu

Once this is selected, you can go to the “Robots” scripts, scroll down to the bottom and you will find the communication blocks. You might need to click “Show Extension”:

show-extensison

The communication blocks look like this:

comms-options

Communication between your robot and your computer happens in the window on the right hand side of the mBlock programming environment:

serial-comms-window

Programming:

The current value of sensors can be displayed through this communication. To display the current value, use the “Write line” block:

write-line-block

Replace the input “hello” with the ultrasonic sensor value and put it in a forever loop:

write-line-loop

When I run this file, I got the following output:

write-feedback

Your robot can also be controlled by reading from the input line. First check that data is available:

data-available-block

If it is, then read the line with this:

read-line-block

And compare it with this:

is-equal-block

Finally, program your robot to behave according to your wishes. In this program, an input of “r” will turn the LEDs red, and “o” will turn them off:

led-control

Then, in the input area, I have turned the lights on red, off, and back on red again:

turn-leds-on-and-off

Challenge:

  1. Write a program that controls the motors with serial communication.

Construct Your Dreams!