Measuring Distance

Since my final project's only input device is a button, which I already got working on Week 6, this week I will:

  • communicate with a VL53L1X Time-Of-Flight sensor via I2C using a Seeed Studio XIAO-RP2040 microcontroller
  • mill a PCB board

Planning

I asked ChatGPT 3.5 to help me make a plan.

Prompt

This week for an engineering class I’m taking, I have to communicate between a microcontroller and an input device on a pcb board I design and mill. I have a XIAO Seeed Studio RP2040 and a VL53L1X, and I know how to use EagleCAD. Make me a detailed procedure of how I can finish this is 2hrs

Response

Sure, I can provide you with a detailed procedure for your project. However, I need to inform you that the process of designing and milling a PCB board is a time-consuming process and may take more than two hours. Nevertheless, I will provide you with a step-by-step procedure for completing your project as efficiently as possible.

Step 1: PCB Design

Launch EagleCAD software and create a new schematic.
Add the XIAO Seeed Studio RP2040 and VL53L1X components to the schematic.
Connect the components using the appropriate pins.
Create a board layout from the schematic.
Place the components on the board layout and arrange them in a logical and optimal manner.
Route the connections between the components using the appropriate traces.
Verify the design for errors, and then generate the necessary files for milling the board.
Step 2: Milling the PCB Board

Load the generated files into the milling software.
Insert the blank PCB board into the milling machine.
Set the milling parameters, such as depth, speed, and tool size.
Start the milling process and wait until it is complete.
Inspect the board for any errors or issues.
Step 3: Microcontroller Programming

Connect the XIAO Seeed Studio RP2040 to a computer using a USB cable.
Download and install the necessary drivers and software for the microcontroller.
Write the code for communicating with the input device (VL53L1X) using the microcontroller.
Upload the code to the microcontroller and verify its functionality.
Step 4: Circuit Testing

Connect the input device (VL53L1X) to the microcontroller using the appropriate pins.
Power on the circuit and test the functionality of the input device using the microcontroller.
Verify that the communication between the microcontroller and the input device is working as intended.
In conclusion, designing and milling a PCB board can be time-consuming, but with the right tools and techniques, it is possible to complete the project in two hours. Remember to double-check your design for errors before milling the board and test the circuit thoroughly before submitting it. Good luck with your project!

Prompt

Expand step 3 to be more detailed 

Response

Sure, here is a more detailed procedure for Step 3, which involves programming the microcontroller:

Connect the XIAO Seeed Studio RP2040 to a computer using a USB cable.

Plug one end of the USB cable into the microcontroller and the other into a USB port on your computer.
Download and install the necessary drivers and software for the microcontroller.

Go to the Seeed Studio website and download the necessary drivers for the XIAO RP2040.
Install the drivers on your computer.
Install the Integrated Development Environment (IDE) for the RP2040.

Download and install the Arduino IDE from the official website.
After installation, launch the IDE.
Select the board and port.

Open the Tools menu and select the board type "Seeeduino XIAO (M0)" from the list of boards.
Next, select the appropriate serial port in the Tools menu.
Install the libraries.

Install the necessary libraries for the VL53L1X sensor. You can find the libraries in the Arduino library manager or download them from the manufacturer's website.
Write the code for communicating with the input device (VL53L1X) using the microcontroller.

Open a new sketch in the Arduino IDE.
Import the necessary libraries by going to Sketch > Include Library > VL53L1X.
Write the code to initialize the sensor, read the sensor data, and communicate with the microcontroller.
Test the code to make sure it is functioning correctly.
Upload the code to the microcontroller and verify its functionality.

Connect the XIAO Seeed Studio RP2040 to your computer using the USB cable.
Compile and upload the code to the microcontroller by clicking the "Upload" button in the Arduino IDE.
Open the Serial Monitor to view the output of the sensor data and verify that the microcontroller is communicating correctly with the input device.
By following these steps, you can program your microcontroller and ensure that it is correctly communicating with the input device. Remember to test your circuit thoroughly and verify that it is working as intended.

Prompt

1. How can I install that specific board In the arduino ide? 2. Which specific library do I install? 3. How do I wire it (you can give me some example code for this step)?

Response

Sure, I can provide more detailed information for you.

How to install the Seeeduino XIAO (M0) board in Arduino IDE:

Open the Arduino IDE and go to "File" > "Preferences"
In the "Additional Boards Manager URLs" field, paste this link: https://files.seeedstudio.com/arduino/package_seeeduino_boards_index.json
Click "OK"
Go to "Tools" > "Board" > "Boards Manager"
In the "Boards Manager" window, search for "Seeeduino SAMD Boards"
Click "Install"
Once the installation is complete, you can select "Seeeduino XIAO (M0)" under "Tools" > "Board"
Which specific library to install:

For the VL53L1X sensor, you can use the "VL53L1X" library from STMicroelectronics.
To install the library, go to "Sketch" > "Include Library" > "Manage Libraries"
In the Library Manager, search for "VL53L1X" and click "Install"
How to wire the Seeeduino XIAO to the VL53L1X sensor:

VCC on the sensor connects to 3.3V on the Seeeduino XIAO
GND on the sensor connects to GND on the Seeeduino XIAO
SDA on the sensor connects to pin 20 (SDA) on the Seeeduino XIAO
SCL on the sensor connects to pin 21 (SCL) on the Seeeduino XIAO
Here is an example code for reading data from the VL53L1X sensor:
#include <Wire.h>
#include "Adafruit_VL53L1X.h"

Adafruit_VL53L1X lox = Adafruit_VL53L1X();

void setup() {
  Serial.begin(9600);
  while (!Serial) { delay(10); }

  Wire.begin();
  lox.setTimeout(500);

  if (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L1X"));
    while(1);
  }

  Serial.println(F("VL53L1X ready!"));
}

void loop() {
  VL53L1_RangingMeasurementData_t measure;

  lox.rangingTest(&measure, false);

  if (measure.RangeStatus != 4) {
    Serial.print(F("Distance (mm): "));
    Serial.println(measure.RangeMilliMeter);
  } else {
    Serial.println(F("Out of range"));
  }

  delay(500);
}
You can upload this code to your Seeeduino XIAO and monitor the serial output to see the measured distance from the VL53L1X sensor. Make sure you have installed the necessary libraries before uploading the code.

Electronics

Libraries

I followed this steps given by ChatGPT 3.5 above.

How to install the Seeeduino XIAO (M0) board in Arduino IDE:

1. Open the Arduino IDE and go to "File" > "Preferences"
2. In the "Additional Boards Manager URLs" field, paste this link: https://files.seeedstudio.com/arduino/package_seeeduino_boards_index.3. json
4. Click "OK"
5. Go to "Tools" > "Board" > "Boards Manager"
6. In the "Boards Manager" window, search for "Seeeduino SAMD Boards"
7. Click "Install"
8. Once the installation is complete, you can select "Seeeduino XIAO (M0)" under "Tools" > "Board"

Differences:

  • Seeed SAMD Boards not Seeeduino SAMD Boards in the Boards Manager
  • Seeeduino XIAO not Seeeduino XIAO (M0) when selecting the board

I next followed these steps.

1. Go to "Sketch" > "Include Library" > "Manage Libraries"
2. In the Library Manager, search for "VL53L1X" and click "Install"

Differences

  • Adafruit VL53L1X not VL53L1X by Pololu because the code provided matches that better
  • I already had it installed

Programming

ChatGPT wrote this code.

#include <Wire.h>
#include "Adafruit_VL53L1X.h"

Adafruit_VL53L1X lox = Adafruit_VL53L1X();

void setup() {
  Serial.begin(9600);
  while (!Serial) { delay(10); }

  Wire.begin();
  lox.setTimeout(500);

  if (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L1X"));
    while(1);
  }

  Serial.println(F("VL53L1X ready!"));
}

void loop() {
  VL53L1_RangingMeasurementData_t measure;

  lox.rangingTest(&measure, false);

  if (measure.RangeStatus != 4) {
    Serial.print(F("Distance (mm): "));
    Serial.println(measure.RangeMilliMeter);
  } else {
    Serial.println(F("Out of range"));
  }

  delay(500);
}

Wiring

I followed these instructions on a breadboard.

- VCC on the sensor connects to 3.3V on the Seeeduino XIAO
- GND on the sensor connects to GND on the Seeeduino XIAO
- SDA on the sensor connects to pin 20 (SDA) on the Seeeduino XIAO
- SCL on the sensor connects to pin 21 (SCL) on the Seeeduino XIAO

Breadboard Test

I didn't see any options other than COM3 under Port so I unplugged and replugged the board holding the B (Bootloader) button. Then it was still only COM3, so I tried uploading to that. I got this error:

Compilation error: 'class Adafruit_VL53L1X' has no member named 'setTimeout'; did you mean 'setTimingBudget'?

I tried this code from Adrian Torres' fabXIAO website.

#include 
#include 

VL53L1X sensor;

void setup()
{
  Serial.begin(115200);
  Wire.begin();
  Wire.setClock(400000); // use 400 kHz I2C

  sensor.setTimeout(500);
  if (!sensor.init())
  {
    Serial.println("Failed to detect and initialize sensor!");
    while (1);
  }

  // Use long distance mode and allow up to 50000 us (50 ms) for a measurement.
  // You can change these settings to adjust the performance of the sensor, but
  // the minimum timing budget is 20 ms for short distance mode and 33 ms for
  // medium and long distance modes. See the VL53L1X datasheet for more
  // information on range and timing limits.
  sensor.setDistanceMode(VL53L1X::Long);
  sensor.setMeasurementTimingBudget(50000);

  // Start continuous readings at a rate of one measurement every 50 ms (the
  // inter-measurement period). This period should be at least as long as the
  // timing budget.
  sensor.startContinuous(50);
}

void loop()
{
  Serial.print(sensor.read());
  if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }

  Serial.println();
}

I left the old include statements since the ones were missing parts on the website.

I got this error.

Compilation error: no matching function for call to 'VL53L1X::VL53L1X()'

So I tried changing the import statements.

#include <Wire.h>
#include "Adafruit_VL53L1X.h"

Here's the code overall.

#include <Wire.h>
#include "VL53L1X.h"

VL53L1X sensor;

void setup()
{
  Serial.begin(115200);
  Wire.begin();
  Wire.setClock(400000); // use 400 kHz I2C

  sensor.setTimeout(500);
  if (!sensor.init())
  {
    Serial.println("Failed to detect and initialize sensor!");
    while (1);
  }

  // Use long distance mode and allow up to 50000 us (50 ms) for a measurement.
  // You can change these settings to adjust the performance of the sensor, but
  // the minimum timing budget is 20 ms for short distance mode and 33 ms for
  // medium and long distance modes. See the VL53L1X datasheet for more
  // information on range and timing limits.
  sensor.setDistanceMode(VL53L1X::Long);
  sensor.setMeasurementTimingBudget(50000);

  // Start continuous readings at a rate of one measurement every 50 ms (the
  // inter-measurement period). This period should be at least as long as the
  // timing budget.
  sensor.startContinuous(50);
}

void loop()
{
  Serial.print(sensor.read());
  if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }

  Serial.println();
}

Then I uploaded and got an error that COM3 wasn't able to recieve a sketch.

I tried plugging in against without holding the bootloader button, then I saw the port!

I then got this error when I tried to upload.

o upload port found, using address:"COM28" label:"COM28" protocol:"serial" protocol_label:"Serial Port (USB)" properties:{key:"pid" value:"0x0005"} properties:{key:"serialNumber" value:"4150323038323716"} properties:{key:"vid" value:"0x2E8A"} as fallback
"C:\Users\adams\AppData\Local\Arduino15\packages\Seeeduino\tools\bossac\1.7.0-arduino3/bossac.exe" -i -d --port=COM28 -U true -i -e -w -v "C:\Users\adams\AppData\Local\Temp\arduino-sketch-9ADA224C2F3BD18DF5A01F047CDE785D/sketch_apr17a.ino.bin" -R
No device found on COM28
Set binary mode
Send auto-baud
Set binary mode
Failed uploading: uploading error: exit status 1

Based on this website, I tried holding down the bootloader button as I hit Upload in the Arduino IDE, but this didn't help.

I tried using the website's board link (same steps as before).

https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json

But I didn't get an option to press Install, so I restarted Arduino IDE. I also tried deleting the other URLs since this had caused issues in the past. Now I saw the option, although the button was partially cut off.

cut off button

I selected Seeed XIAO RP2040.

I got a weird error and realized that it had to do with a strange character caused from copying and pasting, so I deleted and hand-wrote the imports.

Compilation error: stray '#' in program

I tried commenting the ends of the lines out, trying to upload (failing), then undoing it, and then it compiled - weird. I got an error when it was uploading.

Scanning for RP2040 devices
No drive to deploy.
Failed uploading: uploading error: exit status 1

I tried again holding down the bootloader button and got this error again.

Compilation error: stray '#' in program

I deleted then retyped the # characters per this tutorial, and it compiled again. I tried unplugging and replugging with the bootloader button, and now I saw an option for UF2 Board under Port. Then it uploaded, but I didn't see any output in the serial monitor. I then opened Port again and there was COM31 and no UF2 Board, and when I selected this and went to the serial monitor, the sensor worked!

So, from my understanding, here is the final workflow.

  • plug in without bootloader button
  • select board + board
  • upload (it will fail)
  • unplug
  • plug in while holding bootloader button
  • select UF2 Board as the upload port
  • upload
  • select the new port which will be COM something
  • open the serial monitor (if applicable)

PCB

Board Design

I used the XIAO-RP2040 component from the Seeed Studio XIAO Series (already was installed for me). I also used the 1X5 component from the adafruit library (already installed, as well). I chose 5 headers so I could have a separate pin for VIN and 3V3 depending on whether I was powering the microcontroller over USBC or with a separate power source. I also made sure to make the design to where the USBC plug is right at the edge of the board because, last time, it was very hard to plug in. This design will serve as a general-purpose barebones Seeed Studio XIAO I2C development board.

Schematic

For autorouting, I changed the minimum trace width to 40mil and the minimum distance for copper/dimension to 40mil. I also set Bottom to N/A so it wouldn't design a 2-sided board. I also realized that on the schematic GND wasn't actually touching the pin. I also had to slightly increase the size of the board so the routing would be possible. I also set wire-to-wire clearance to be 20mil. For some reason the top autorouted result was displayed as 80%, so I selected one of the other results that was 100%. I also might not be able to solder the connection on the underside of the XIAO, but that is sufficient for this week.

Layout

I exported the files for milling as usual.

Board Production

I downloaded the files onto the machine and followed the same steps as in previous weeks to mill the board. I started off by using the larger Bantam Milling machine, which I had never used before. At first it got stuck saying Initializing..., so I turned it off and on. I also noted that I had to move the bit very far out of the collet otherwise the z-axis probing wouldn't reach. The milling did not turn out well, and I figured I might of had some issue with a depth setting or the calibration, so I decided to use the other machine.

failed board

When I did it with the other machine, it worked better.

board shot 1

board shot 2

Soldering

I decided not to solder the conncetion for VIN on the bottom of the microcontroller. Dr. Fagan told me this is possible using a heat gun and solder paste, but I will not end up using this feature this week anyways.

I noticed the footprint I had used for the header pins didn't have the same spacing as the header pins I had, so I made the individual pieces for now, but in the future I'd like to redesign the board to work better and ensure the traces won't rip as easily.

soldered hd

I put insulating tape on the back of the microcontroller to make sure it won't short circuit, then I soldered it.

tape

soldered

Testing

The board worked first try! When uploading I got the same error about # so I deleted and retyped the two again, then it uploaded (without pressing bootloader), and I didn't have to go through the entire process outlined above.