Skip to main content

Project Based Engineering Instrumentation High Level Coding and Microcontrollers

Section 22.1 CPX/CPB Wiring and Software

The wiring for this system requires 4 wires. The figure below shows the IMU connected to Feather M4 (Courtesy of Bryan Siepert)[32].
Figure 22.1.1. Wiring diagram of IMU connected to a FeatherWing[42]
The only difference between the wiring diagram above and the CPX/CPB is that you will be using 4 alligator clips. The rest is straightforward. You need 3.3V to run to VIN, GND to GND and then SDA to SDA and SCL to SCL.
Figure 22.1.2. Photo of IMU Connected to a CircuitPlayground Express
The photo above uses a combination of alligator clips and male to male wires into a breadboard. The red alligator clip is connected to the 3.3V pin on the CPB and then also connected to the red male-male wire which is connected to the VIN pin on the IMU. The black alligator clip is connected to the GND pin on the CPB and also connected to the brown male-male wire which is connected to the GND pin on the IMU. The blue alligator clip is connected directly to the SDA pins on both the CPB and the IMU. The white alligator clip connected to the yellow male-male wire which is connected to the SCL pins on both the CPB and the IMU. Recall that SDA and SCL are external pins for I2C. In the accelerometer lab we used the internal I2C pins to access the accelerometer (See Subsectionย 15.1.4). In this lab weโ€™re going to use the external I2C pads to connect different I2C sensors.
Once you have the circuit wired and soldered itโ€™s time to work on software. First, you want to make sure you have your Circuit Python UF2 up to date[30]. In this example Iโ€™m using the 6.X version. Once I updated my UF2 I also updated my Circuit Python Libraries (See Sectionย 7.2 for help on installing extra modules on your CPX/CPB)[30]. The specific modules I needed for this lab were:
  1. adafruit_bus_device
  2. adafruit_register
  3. lis3mdl
  4. lsm6ds33
Once you have the necessary modules you can run some example code. The Adafruit Learn page has a tutorial for the LSM6DS33[32]. The problem with the tutorial is that it seems like it was written for the Raspberry or some other microcontroller. As such I had to find some example code on Adafruitโ€™s Github[33]. After following both tutorials I was able to make my own script and upload it to my Github[33]. Note that itโ€™s a good exercise when attaching I2C devices to run a scan on all connected I2C devices to make sure you can see the device and that you have the correct address (See Sectionย 7.4).
Figure 22.1.3. CircuitPython code to read an IMU
In the code above lines 1-6 import all the modules with line 5 importing the accelerometer on board the CPX and line 6 importing the external sensor wired up to SDA and SCL. Line 8 creates an I2C object using the SDA and SCL pins from the alligator clips and line 11 creates the sensor object. I also include lines 14-17 to include the onboard accelerometer. Notice I can access both sensors no problem. In the while loop line 20 checks the accelerometer on the CPX, line 21 checks the accelerometer on the breakout board and line 22 checks the angular velocity on the breakout board. Lines 23 and 24 print to serial and output to the plotter. Note that some lines are commented out because I wanted to try one thing at a time. With both accelerometers printing to the Plotter I could move the CPX and the breakout board in unison and get the following output.
Figure 22.1.4. Serial monitor open showing IMU data in Mu
Notice that there are 6 numbers printed and 6 lines on the plotter. Both the CPX and the breakout board have little XYZ cartesian coordinate systems. I had to line them up properly before I started moving them. My suggestion would be for you to get some hot glue or 3M tape and place both breakout board and CPX on some sort of hard material like plywood, masonite, or even a cutting board. Anything to keep everything together.
Once youโ€™ve done this, try uncommenting the line of code that prints the angular velocity. When I do that and move the breakout board around I can measure the angular velocity of each axis. The units are in radians per second but itโ€™s pretty obvious just from the magnitude of the graph.
Figure 22.1.5. Angular Velocity data of IMU shown in Serial Monitor or Mu
The final part is to get the LIS3MDL (magnetometer) to work. The starting point for me was the Adafruit Learn page, along with the simple example from Adafruitโ€™s Github. After that I was able to create my own code[33][32]. The only difference in your code is that the address will be 0x1c instead of 0x1e.
Figure 22.1.6. Magnetometer code in CircuitPython
The code is almost identical to the code before except all the LIS3DH and LSM6DS33 code is commented out. Instead I have code to grab the magnetometer (LIS3MDL) at address 0x1E. Line 25 then calls the magnetometer and prints it.