Digital Sparkle Collar

2025-Apr-02, Wednesday 01:00 am
History )

Collar )

Circuit )

Attachment )

Putting it all together )

Finishing touches )

Coding )

Testing )


Photo of the neopixel collar, closed, and turned on, with four wires meeting in the middle, leading to the Gemma in a translucent bright green 3d printed circular case, inside yet another in a translucent bright green 3d printed circular case, with the LEDs in various shades of green, yellow, white, and off.

Digital Sparkle Collar, with more robust case situation
Photo by [personal profile] chebe

Compare and contrast getting normalized accelerometer values, where the raw value is -13248.00. (A.k.a. Please, make it make sense.)

Library 1; jarzebski's Arduino-MPU6050 - https://github.com/jarzebski/Arduino-MPU6050/
Library 2; Adafruit's Adafruit_MPU6050 - https://github.com/adafruit/Adafruit_MPU6050
Library 3; i2cdevlib/ElectronicCats' mpu6050 - https://github.com/ElectronicCats/mpu6050


Library 1;


code )

Formula for normalized acceleration value at MPU6050_RANGE_2G;
raw * rangePerDigit * gravityConst
(raw * (1.0/16384)) * gravityConst
== (raw / 16384) * gravityConst
(-13248.00 * .000061) * 9.80665 = -7.92502845
== (-13248.00 / 16384) * 9.80665 = -7.92959589


Library 2;


code )

Formula for normalized acceleration value at MPU6050_RANGE_2_G;
(raw / accel_scale) * gravityConst
(-13248.00 / 16384) * 9.80665 = -7.92959589


Library 3;


code )

No normalized functions in MPU6050.cpp. Can use MotionApps files, but they go through Quaternions.
code )

Formula for normalized acceleration value at MPU6050_ACCEL_FS_2;
// jarzebski way;
raw * accelerationResolution * gravityConst
-13248.00 * 0.000122 * 9.80665 = -15.850057

// adafruit way;
// (raw * (1.0/16384)) * gravityConst
// (raw * (2.0/16384)) * gravityConst
(-13248.00 / (2.0*16384)) * 9.80665 = -15.85919
((-13248.00*2.0) / 16384) * 9.80665 = -15.85919

Result is twice that of the others. accelerationResolution itself seems to be twice the others, so half it? Not sure if this is a mistake, or I'm missing something about MPU6050_ACCEL_FS_2 vs MPU6050_RANGE_2_G.
raw * (accelerationResolution/2) * gravityConst
-13248.00 * (accelerationResolution/2) * 9.80665 = -7.9250

accelerationResolution and get_acce_resolution() only exist in the ElectronicCats version, they're not accessible in the i2cdevlibs version. That version seems to really want you to use the Quaternion path. But you can always use the range values from the other versions yourself on the raw values.
The MPU-6050 is an accelerometer and gyroscope (with added thermometer), that contains a Digital Motion Processor (DMP) to handle a lot of the heavy maths lifting of turning the raw sensor values into useful motion values before they reach the microcontroller. It seems (caveat; I'm new to this area) that the original code is only available as a binary, or as some demo code. But some very determined people have pulled a library together.

/* Source is from the InvenSense MotionApps v2 demo code. Original source is
 * unavailable, unless you happen to be amazing as decompiling binary by
 * hand (in which case, please contact me, and I'm totally serious).
 *
 * Also, I'd like to offer many, many thanks to Noah Zerkin for all of the
 * DMP reverse-engineering he did to help make this bit of wizardry
 * possible.
 */


Well, actually, it seems quite a lot of people have pulled libraries together, to different levels of functionality, with very similar names. But the one most people seem to use is this one. Which includes the MotionApps files. This is important, not just for the right function calls, but because you need other libraries from this collection as well, particularly the I2Cdev one. These libraries, although for Arduino, are not findable inside the Arduino IDE's Library Manager. You need to side-load them, old school style. Which means downloading and extracting the .zip file. Then copying the specific libraries (being the folders that contain an 'Examples' folder as a first level child) for MPU6050 and I2Cdev into your Arduino > libraries folder. And restarting the IDE.

(I've also, after the fact, found what seems to be a copy of the MPU6050 library in an ElectronicCats repo, that is in the Library Manager, and seems to have rolled the I2Cdev code into itself. But the oldest change here seems to be 6 years ago. Whereas the i2cdevlib seems to be about 13 years ago. It does seem to have more recent updates than i2cdevlib though. So the choice is yours. I'm telling you, it's a nightmare finding what's what, especially when the code linking to it won't even compile with it.)

To help us get a feel for how the boards movements are interpreted, there's a nice demo we can try out. You can find the MPUTeapot Processing project inside the MPU6050 Arduino library; MPU6050 > examples > MPU6050_DMP6 > Processing > MPUTeapot.

First step is to install Processing. I'm using P4. Copy that MPUTeapot.pde file into a new sketch (or the whole folder to wherever you save your sketches). This sketch requires another stack of libraries, the Toxi/c libraries, which we need to side-load again. Download the newest release, extract the .zip file, and copy the libraries (being the folders that contain an 'examples' folder as a first level child) into your Processing > Libraries folder. Restart Processing.

Illustration of Arduino Mega board wired up to MPU6050 board as described below.

MPU6050 Circuit
Made with Fritzing



Wire up your MPU6050 to your Arduino (I'm using the Mega 2560). You need to connect the Interrupt pin to Digital pin 02 (on most Arduino boards). SDA -> SDA. SCL -> SCL. VCC -> 5V. GND -> GND.

Open the MPU6050 > MPU6050_DMP6 example, and load it onto your Arduino. By default it runs in readable yaw-pitch-roll mode. Open the Serial Monitor at 115200 baud. After finding and initialising your MPU6050 it will await char input. Send any char, and you'll get a lot of lines like;
DMP ready! Waiting for first interrupt...
ypr	0.01	-0.01	0.44
ypr	0.01	-0.01	0.44
ypr	0.01	-0.01	0.44
ypr	0.01	-0.01	0.43

Which are your yaw-pitch-roll Euler angles in degrees.

If you comment out the #define OUTPUT_READABLE_YAWPITCHROLL line, and uncomment the #define OUTPUT_TEAPOT line you'll prepare the code for use with the Processing demo. Instead of nice readable yaw-pitch-roll values the info is packed into a 42-byte FIFO packet buffer; [quat w][][quat x][][quat y][][quat z][][gyro x][][gyro y][][gyro z][][accl x][][accl y][][accl z][][]. Upload to your Arduino.

Moving over to Processing now, open the MPUTeapot demo. I find it nostalgic that this is running in OpenGL mode. If you want to increase the window size, go ahead and do that. Otherwise you just need to set the correct portName. For me that meant commenting out line 72, and uncommenting line 75, while updating line 75 to say "COM7". The demo is straight-forward, handling the drawing of the virtual plane, reading and unpacking the packets back to quaternion w,x,y,z values, and handing off the complications to the Toxi/c libraries. But when it works it's a lot of fun to manipulate the virtual plane by moving your MPU6050. And really helps you connect the board movement to what you want your project to detect/react to, in your mind.

Animation of a block plane (red board, green wings and tail fin, blue nose cone) rotating in 3D space.

MPUTeapot demo
Gif by [personal profile] chebe

MG90D Servos with Arduino

2025-Mar-12, Wednesday 12:00 pm
Take one Arduino Uno, and run some servos. Seemed straight-forward. Seemed.

Firstly, I couldn't find any Unos or Duemilanoves, I must have used them all. Guess it's time to finally break out the Mega 2560.

The servos are the MG90D micro servos. And using the Servo.h library.

Basic circuit set up like the sweep tutorial; on a breadboard, using a 100 µF capacitor (servo is 4.8V to 6V DC voltage), and external 9V power supply. Sweep worked fine, but setting exact angles via write() would only sometimes work.

Illustration of Arduino Mega board, with 9V power source, connected to a servo with power through a breadboard with capacitor, and connected to Digital/PWM pin 09.

Servo Circuit
Made with Fritzing



After some poking around I learned that I needed to set the minimum and maximum pulse lengths for these particular servos. (The values from the product description on Adafruit. Also note, this servo will 'hold' the position even when no signal is sent.) And then I could set the degree of rotation (0°-180°) as expected.


#include <Servo.h>

Servo myServo;

int minPulseWidth = 750; // µs == 0.75ms
int maxPulseWidth = 2250; // µs == 2.25ms

void setup()
{
  myServo.attach(9, minPulseWidth, maxPulseWidth); // digital pin (with PWM) 9
}

void loop()
{
  myServo.write(90); // angle in degrees, 0-180
  delay(1000); // ms
  myServo.write(180); // angle in degrees, 0-180
  delay(1000); // ms
  myServo.write(0); // angle in degrees, 0-180
  delay(1000); // ms
}
I went to an event at the weekend. Which meant I needed an event badge. Time to dust off my HalloWing badge. I finished the last post saying I would update once I got the badge reading images from the SD card shield. Well, dear reader, I was pleasantly surprised to discover that at some point I had gotten it working! At least to the point of auto-rotating through all the images on the card.

I don't really remember what I did to get the SD card working, but from the code the main bits are;
#include <SPI.h>
#include <SD.h>

const int chipSelect = 10;
File root;

void setup(void) {
  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  // List files on the SD card
  root = SD.open("/");
  printDirectory(root, 0);
  root.close();
}

printDirectory(root, 0) uses dir.openNextFile(); which sorts through your files alphabetically. bmpDraw(...) (see code below) is what what reads the bmp file and translates it into what the tft wants. Nice.

But I wanted more. First thing you need to know is that Adafruit have moved on and really want us to use the Arcada library to work with the badges in Arduino. But if I wanted a simple life I would have gone with the CircuitPython option. I travelled the frustrating path instead.

I set up two of the buttons to 'scroll' left/right through the images. The images have overlays that display matching usernames. One of the buttons toggles on/off the tft backlight, and the other button toggles on/off the on-board NeoPixel. For a while things weren't working, and after a lot of digging I discovered that Adafruit had made breaking changes, including deprecating a function they had used in the examples I was working from.

setAddrWindow(...) was changed, from (start_x, start_y, end_x, end_y) to (start_x, start_y, width, height). So the change was from;
tft.setAddrWindow(x, y, x+w-1, y+h-1);
to;
tft.startWrite();
tft.setAddrWindow(x, y, w, h);
tft.endWrite();


pushColor(...) is deprecated. After experimentation I discovered that;
tft.pushColor(tft.color565(r,g,b));
needs to be replaced with;
tft.startWrite();
tft.writePixel(col, row, tft.color565(r,g,b));
tft.endWrite();


For the buttons a library is required;
#include <Adafruit_FreeTouch.h>
Then you need to create each of the four buttons (A2, A3, A4, A5);
Adafruit_FreeTouch qt_1 = Adafruit_FreeTouch(A2, OVERSAMPLE_4, RESISTOR_50K, FREQ_MODE_NONE);
Each of which you qt_1.begin(); in setup(void), and then read with qt_1.measure() as in;
if (qt_1.measure() > 700) { /* do stuff */ }

The backlight and NeoPixel parts were straight-forward. And the only other thing I did was add an extender to the battery JST connector so it's easier to reach. I wrapped the leads around the shield, and tucked the battery between the shield and board.




Back of the badge, with SD card shield in place, and battery tucked between
Photo by [personal profile] chebe






Front of the badge, displaying an image with overlay, and NeoPixel backlit
Photo by [personal profile] chebe



Messy code dump cobbled together from various tutorials )
Time to play with some super bright LEDs! (Just remember; do not look directly at them while powered.)

Kitronik have some lovely 3W LEDs (with unfortunate name) in a star shape for heat dissipation. They provide a datasheet with example circuit for 5V power source. It requires 2.2 ohm power resistors, which are new to me. But when I tried regular resistors there was a lot of smoke, so they are definitely required.

I want to control the lights from a lower voltage micro-controller, so I'm going to need some N-channel MOSFETs. This is my first time using these. The Data pin (from the Arduino) acts a switch to complete the higher power circuit that lights up the LED.

All together the circuit looks a bit like this.

Images )
An Arduino microcontroller can generate a square wave on PWM pins to simulate an analog wave. But the wave alternates between 5V and 0V, it doesn't change polarity.

If you have a component that expects an AC signal you need to change the 0V to 5V signal to a -5V to 5V signal.

One simple way to do this is with a SparkFun Transceiver Breakout. Connect the Power pin to either 3.3V or 5V on the Arduino, GND to GND, T2IN to a PWM pin, and T2OUT to your component. analogWrite() your required frequency to control your component.



Back of SparkFun Transceiver Breakout with labelled pins visible
Photo by [personal profile] chebe

Time Cuboid

2020-Apr-28, Tuesday 08:20 pm
I find myself, like many of us, sitting at my desk a lot. I sit there for work. I sit there again for the many video and/or voice socials that have been set up. I sit there to play games or watch films. I even clear off the computer stuff and sit there to solder and work on projects. I am, in fact, sitting there now as I write this. So basically, I sit there a lot. And it got so that I found it difficult to know what time it was, whether early or late, start of the day, or end. I know, I thought, possibly aloud, no-one will ever know, I need a clock.

But I didn't want to go buying lots of unnecessary things, or wait until my usual suppliers were back in full swing. So I dug around in the piles of boxes that comprise my electronics stash. Back at GaelHack (seven years ago) I started on a clock, but it never left the breadboard. I dusted that off, and found most of what else I needed.

Making it all fit together )

Given the purpose of the device, and age of most of the components, I had half a mind to refer to this as a Time Capsule, but Time Cuboid seems more self-explanatory.
Overall it looks something like this;



Time Cuboid, on
Photo by [personal profile] chebe

Mega Pro Mini 3.3V

2017-Sep-23, Saturday 10:00 pm
I finally opened up my Mega Pro Mini 3.3V, long retired it turns out, for something. Luckily I had bought the necessary connectors at the same time, and I always have an FTDI breakout board handy. But things I needed to figure out;

You need to add Sparkfun to your "Additional Boards Manager URLs" in the Arduino IDE;
https://raw.githubusercontent.com/sparkfun/Arduino_Boards/master/IDE_Board_Manager/package_sparkfun_index.json

Then select your Board as "SparkFun Mega Pro", and Processor as "ATmega2560 (3.3V / 8 MHz)".

But also, that each of the pins in output mode only supply about 40ma. Not the 200ma these lamps require. *sigh* Back to LEDs!

Open Theremin V3

2017-Feb-26, Sunday 06:24 pm
Gather:
- active/powered speakers with a 3.5mm stereo audio jack (bonus points if the power plug is grounded)
- camera tripod
- Arduino Uno, from wherever it has been gathering dust (and USB/power cable)

Acquire:
- Open Theremin Arduino Shield
- 1m of aluminium round tube; 6mm external thickness, 1mm wall thickness, 4mm internal diameter, from your local hardware shop

Do:
- Cut tube in half, and bend (or get bent) each half into the shape described in the diagram
- Solder shield together. (The button and LEDs are surface mounted. I scorched a leg pad of the yellow LED, so no blinky yellow for me.)
- Program Uno
- Plug all the bits together. The whole lot should be grounded, but both my speakers, and the Arduino, are powered over USB, and I could not find an earthed USB-converter plug. So, at the moment, they are plugged into my laptop, which is plugged into the mains. (There is a ground pad on the shield that I should hook up in future.)
- Power up, leave to warm up, trigger auto-calibration
- Make ALL THE NOISE



Theremin!
Photo by [personal profile] chebe



- Start band