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.

[pastacode lang=”c” manual=”void%20setup()%20%7B%20%0A%20%20%20%20%2F%2F%20put%20your%20setup%20code%20here%2C%20to%20run%20once%3A%0A%20%20%20%20Serial.begin(115200)%3B%0A%7D%0A%0Avoid%20loop()%20%7B%0A%20%20%20%20%2F%2F%20put%20your%20main%20code%20here%2C%20to%20run%20repeatedly%3A%0A%20%20%20%20Serial.println(%22HA%20HA%20I’ve%20unlocked%20the%20power%20of%20the%20MicroBit%22)%3B%0A%20%20%20%20delay(500)%3B%0A%7D” message=”Some Test Code” highlight=”” provider=”manual”/]

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.

[pastacode lang=”c” manual=”%23include%20%3CNewPing.h%3E%0ANewPing%20sonar(15%2C%2015%2C%20400)%3B%20%2F%2Fcreate%20a%20newPing%20object%20using%20pin%2015%20for%20trig%20and%20echo%20and%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2Fsetting%20the%20max%20distance%20to%20400CM%20%0Avoid%20setup()%20%7B%0A%20%20%20%20%2F%2F%20put%20your%20setup%20code%20here%2C%20to%20run%20once%3A%0A%20%20%20%20Serial.begin(115200)%3B%20%2F%2FSetup%20the%20serial%20port%0A%7D%0A%0Avoid%20loop()%20%7B%0A%20%20%20%20delay(50)%3B%20%2F%2F%20Wait%2050ms%20between%20pings%20(about%2020%20pings%2Fsec).%2029ms%20should%20be%20the%20shortest%20delay%20between%20pings.%0A%20%20%20%20unsigned%20int%20uS%20%3D%20sonar.ping()%3B%20%2F%2F%20Send%20ping%2C%20get%20ping%20time%20in%20microseconds%20(uS).%0A%20%20%20%20Serial.print(%22Ping%3A%20%22)%3B%0A%20%20%20%20Serial.print(uS%20%2F%20US_ROUNDTRIP_CM)%3B%20%2F%2F%20Convert%20ping%20time%20to%20distance%20and%20print%20result%20(0%20%3D%20outside%20set%20distance%20range%2C%20no%20ping%20echo)%0A%20%20%20%20Serial.println(%22cm%22)%3B%0A%7D” message=”Using the NewPing Library” highlight=”” provider=”manual”/]

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.