In this post, we will set up a ranging application for Arduino boards to display the distances between two DWM1000 modules, one acting as an anchor and the other as a tag. This is why we will need to upload two different sketches into two Arduino boards.
First of all, we have to install the package related the Arduino board we are using. In this example, we will be using an Arduino Zero. To do that, click on Window in the menu toolbar, then Preferences. Select Arduino then Third party index url’s. If the package we want to use is not included by default, we will need to add the url to download it. In our case , we need to add this line:
Now, go to Platforms and Boards and download the correct Arduino board (latest version). In our case, we will be downloading Arduino SAMD Boards (32 bits ARM Cortex M0+). Wait for Sloeber to install it (this might take some time).
Now, we will configure Sloeber to indicate the path of our external libraries. To do that, simply go to Window, then Preferences and click Arduino:
Beside the Private library path field, click on the New… button and browse to the directory you would like to use to store private libraries. In our case, we chose Arduino/libraries folder in Documents. This is the folder where we will put the external libraries to use in sketches.
The library we are going to use is the arduino-dw1000 library which can be found here. Clone the repository or download the archive to your computer. Then, rename the folder containing the library from arduino-dw1000-master to DW1000 and then move it to the private library path, chosen above.
Using this library’s example, we will now create two sketches: one for the anchor and one for the tag. Let’s begin by creating the anchor.
1/ Anchor sketch
The anchor is the stationary module and it will print the distance on a serial terminal. We will talk about serial after we uploaded the anchor sketch since it is not relevant before that.
First, create a new sketch. In the new window, enter the project name. We will simply choose “Anchor”:
Click Next. Now, we need to specify the platform we want to upload the sketch into. Choose the platform folder where your board can be found and then select it. Choose the default upload protocol and fill in the Port name.
Note: We can check the Port name in use by going into the Device Manager and looking under the Ports (COM & LPT). As we can see on the following screen, our Port name is COM9.
Beware: switching the USB port also changes the Port name.
The window should look like this:
Click Next. We will now be able to load a sketch template.
In the drop-down list, select Sample sketch. Then, if we configured Sloeber correctly we should see “DW1000examples” in the bottom box. Expand it and check the RangingAnchor box to load this sketch. Click Finish.
Expand the Anchor folder and open the .ino file. If some lines are underlined, it’s probably because the library is still loading so we just need to wait a little bit.
In this .ino file, some changes must be made to make the system work.
First, the connection pins will most likely not be the same one as we used in the code. We need to check our board’s schematic and save the PIN name connected to the interrupt and the chip select pins. In our case, this is respectively PA09 and PA08. Then, we need to open the file called “variant.cpp” which we can find in the Anchor project, under Core > Variant. Once we open it, we will need to find the Pin number matching our PIN name. In our case, we see that PA09 is pin number 3 and PA08 is pin number 4. Change the connection pins value to the correct ones in the .ino file.
To allow serial functionality, we might need to change SerialUSB to either Serial, Serial1 or Serial2 depending on the Arduino board we are using. Change the delay value from 1000 to 5000 to be able to see the device informations after the upload. After that, open a serial client (we recommand Putty even if Sloeber comes with an integrated serial monitor). Configure it to the correct serial port (the same one you used while creating the project) and the same baudrate written in the code (115200). Wait a bit before opening the session.
Upload the sketch and after it’s done, open the Serial session. After some time, the terminal will display some of the device’s informations:
If nothing is written in the terminal, this can mean two things. Either, there was a problem with the module, or the Serial used is not the correct one. This is most likely the second option, so we need to change every SerialUSB in the .ino file to Serial, Serial1 and Serial2 until we find the correct one.
Now that the anchor is ready, we need to upload the tag sketch to another arduino board.
2/ Tag sketch
For the Tag module, we have to do the same thing but instead of loading the RangingAnchor sketch, we must load the RangingTag sketch.
We must also change the connection pins’ values like we did with the Anchor and also possibly the Serial type.
3/ Calibrating the module for better accuracy
If the two sketches were uploaded successfully, we should be able to see a distance now. Open a serial monitor related to the Anchor and the distance should be displayed on the terminal:
If it is not accurate enough for the range order you are trying to measure, this can be tweaked a little by changing the Antenna Delay . The Antenna Delay is the delay between the time a signal arrives at the receiving antenna and the time the arriving message is time-stamped inside the DW1000. This value will vary slightly between different units of the DW1000 and can be changed to affect precision. Note that distances are not really accurate above 70 meters and below 45 centimeters and if we try to measure outside this span, we might see incorrect values. By default, we assign it to 16470, however this can be changed and tested progressively to match the desired precision.
Instead of using an Arduino board as the host, you can also use a Raspberry Pi like in this article. Mixing the hosts is possible: for instance, you can use an Arduino board for the Tag and a Raspberry Pi for the Anchor, like in this geofencing example.