Ultrasonic Example (Arduino IDE)

In this tutorial we will give Bit:Bot eyes so it can avoid obstacles using an ultrasonic sensor. These sensors use sound to detect objects in the same way that bats do. You can use the PXT editor by importing the sonar library to measure distance with an accuracy of 3mm. However this website was setup to show people how to code rather than how to use a graphical editor; with that in mind my intentions were to write an example in MicroPython utilising the sonar sensor. I have found that this is not possible mainly because the MicroPython DAL offers a time resolution of 1mS where we need a time resolution of 1uS or 1nS for accurate distance measurement with sound.

I also found that the scheduler of the MicroPython DAL is 6mS meaning that if you use the sleep command it will round up your sleep time to the nearest 6mS. For instance sleep(1) will make the board sleep for 6mS, sleep(5) will make the board sleep for 6mS and sleep(7) will make the board sleep for 12 mS, etc. Because of these reasons it is impossible to use the current MicroPython build with the sonar senor to measure distance accurately.

The above issues have now been addressed and it is possible to use ultra sonic sensors with the Micro:Bit and MicroPython however, they can still present some issues and work far better using Arduino IDE.

So instead of using MicroPython I will use the Arduino IDE and a programing language similar to C++ and Java. Using the Arduino IDE we can scrap the MicroBit DAL and use all of the features on the Nordic Semiconductor nRF51822 SOC.

You will need the Bit:Bot sonar sensor found here. This is a HC-SR04 ultra sonic senor running at 40Khz. In””stall it and follow the below instructions.

 

Installing Arduino IDE and setting it up for the Micro:Bit

To install and use Arduino IDE follow the below instructions;

  1. Download and install the Arduino IDE from this link.
  2. Open the IDE and Click File ->  Preferences then paste the following link in the “Additional Boards Manager URLs” field https://sandeepmistry.github.io/arduino-nRF5/package_nRF5_boards_index.json
  3. Click OK then click on Tools -> Board -> Boards Manager.
  4. Paste “Nordic Semiconductor NRF5 Boards” into the search field.
  5. Click on the board then click the install button at the bottom right of the field. This will install the drivers for the chip-set that is on the BBC Micro:Bit.
  6. Now select the board by clicking Tools -> Board -> BBC Micro:Bit. If the board is not in the list make sure that the Nordic Semiconductor NRF5 boards are installed properly from step 4 above.
  7. Click Tools -> Softdevice and select S110. (This softdevice is for Verison 1 of the Micro:Bit, do not use this if you have Version 2)
  8. Now you need to upload a soft device to your MicroBit as this would have been deleted when previous sketches were uploaded. Download microbit-adv.zip , extract it then upload it to your Microbit by dragging the hex file onto it in file manager.

Now you Microbit can be used with the Arduino IDE. You can now access all of the memory available on the chip-set and write much more complex programs. As you can see the Arduino IDE is more complex than the Mu editor. However this IDE can be used to program lots of different controllers and will give you more flexibility when programming devices.

Now that its installed you need to select the serial port that the Microbit is connected to and select the chip-set. To do this click on Tools -> Port and select the correct port (if your unsure disconnect the Microbit and one of the ports will disappear, this is the port that you should select once the Microbit is plugged back in). 

Now copy and paste the below program into the IDE window. Then select Sketch -> Upload to upload it to the Microbit.

Some Test Code
void setup() { 
// put your setup code here, to run once:
Serial.begin(115200);
}

void loop() {
// put your main code here, to run repeatedly:
Serial.println("HA HA I've unlocked the power of the MicroBit");
delay(500);
}

Now select Tools -> Serial Monitor. A window will appear make sure that the baud rate at the bottom right is set to 115200. If everything worked correctly you should have the sentence “HA HA I’ve unlocked the power of the MicroBit” appear every 500mS in the window. If you don’t make sure that the code compiled properly, if it did ensure that the above steps were carried out correctly. If windows firewall asks about allowing openocd.exe (this is the online debugger that uploads the code to the uBit) to access your network click allow. If you have issues go to the Adafruit website by clicking here and follow there tutorial.

 

Installing the newPing Library

Goto this link and download the latest version of the newPing library. Open Arduino IDE and click Sketch -> Include Library -> Add .ZIP library. Now select the newPing library that was downloaded. This library has now been added and can be used to control the Ultrasonic sensor.

Paste the below code into the Arduino IDE, compile and upload to the Micro:Bit, install the sensor and switch the batteries on. Open the Serial monitor and experiment with moving objects towards and away from the sensor.

Using the NewPing Library
#include <NewPing.h>
NewPing sonar(15, 15, 400); //create a newPing object using pin 15 for trig and echo and
//setting the max distance to 400CM
void setup() {
// put your setup code here, to run once:
Serial.begin(115200); //Setup the serial port
}

void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
Serial.println("cm");
}

You will notice that at certain angles the reported distance can be inaccurate. This sensor uses a very narrow waveform meaning that it is highly directional. I find that object at an angle of greater than 30 degrees in respect to the sensor are not seen properly by the sensor.

We can now use the sensor to make Bit:Bot see. In the next tutorial I will port the MicroPython code to C++ and the Arduino IDE this will allow us to use the Ultrasonic sensor for our robot and write path finding algorithms.