This post describes the assembly of a wireless sensor transmitter suitable for music and art performance and installation setups. It has been very reliable for performance. At the time of writing I had to learn that the sensor used here (LSM9DS0 with i2c) is not produced anymore. I will update this post in the near future with a more up-to-date sensor breakout board.
Used Parts:
Huzzah ESP8266 – https://www.adafruit.com/product/2471
*| 9 Degrees of Freedom IMU Breakout – LSM9DS0 – https://www.sparkfun.com/products/retired/1263
but supplied code can be easily updated for the Adafruit 9-DOF Accel/Mag/Gyro+Temp Breakout Board – LSM9DS1 LiPo charger and 3.7 1000mAh LiPo battery.
The LSM9DS0 sensor uses the I2C protocol. It is very straight forward to connect the sensor breakout board and the Huzzah ESP.
The sensor breakout board needs to be powered, so connect Gnd and +3.3V between the two circuits. The digital I2C communication uses two wires, the Data line SDA and a Clock line (SCL).
Below is the Arduino Code that will read the sensor data, format and send the readings as OSC bundles.
Ensure you are setting the Arduino IDE to the Huzzah ESP8266 Board (you might need to add this board in the Arduino Board manager, please see Adafruit overview/tutorials for more info.)
In the code, make sure you are setting the SSID name and password of the network you want to use.
Also ensure that the Wire.begin() contains the pin numbers of the SDA and SCL. Here it is 4 and 5.
[code language=”cpp”]
#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
#include <OSCMessage.h> /// https://github.com/CNMAT/OSC
#include <OSCBundle.h> /// https://github.com/CNMAT/OSC
#include <SPI.h> // Included for SFE_LSM9DS0 library
#include <Wire.h>
#include <SFE_LSM9DS0.h> /// https://github.com/sparkfun/SparkFun_LSM9DS0_Arduino_Library/tree/V_1.0.1
#define SET_OFFSET 12
///////////////////////
// LSM9DS0 I2C Setup //
///////////////////////
// Comment out this section if you’re using SPI
// SDO_XM and SDO_G are both grounded, so our addresses are:
#define LSM9DS0_XM 0x1D // Would be 0x1E if SDO_XM is LOW
#define LSM9DS0_G 0x6B // Would be 0x6A if SDO_G is LOW
// Create an instance of the LSM9DS0 library called `dof` the
// parameters for this constructor are:
// [SPI or I2C Mode declaration],[gyro I2C address],[xm I2C add.]
LSM9DS0 dof(MODE_I2C, LSM9DS0_G, LSM9DS0_XM);
long sendCount = 0;
long frameCount = 0;
//const char* ssid = “BTHub3-PQ5N”;
//const char* password = “78cbae358d”;
const char* ssid = “piano+”;
const char* password = “bbbbbbbb”;
// A UDP instance to let us send and receive packets over UDP
WiFiUDP Udp;
//const IPAddress outIp(192, 168, 1, 95);
const IPAddress outIp(192, 168, 5, 111);
const unsigned int outPort = 10101;
float aX = 0.0f;
float aY = 0.0f;
float aZ = 0.0f;
float gX = 0.0f;
float gY = 0.0f;
float gZ = 0.0f;
float mX = 0.0f;
float mY = 0.0f;
float mZ = 0.0f;
void sendBundleViaOSC();
void getMag();
void getGyro();
void getAccel();
void setup() {
Serial.begin(115200);
pinMode(0, OUTPUT);
digitalWrite(0, HIGH);
pinMode(2, OUTPUT);
digitalWrite(2, HIGH);
Wire.begin(4,5); //set i2c SDA and SCL pins
// Use the begin() function to initialize the LSM9DS0 library.
// You can either call it with no parameters (the easy way):
uint16_t status = dof.begin();
// Or call it with declarations for sensor scales and data rates:
//uint16_t status = dof.begin(dof.G_SCALE_2000DPS,
// dof.A_SCALE_6G, dof.M_SCALE_2GS);
// Set output data rates
// Accelerometer output data rate (ODR) can be: A_ODR_3125 (3.225 Hz), A_ODR_625 (6.25 Hz), A_ODR_125 (12.5 Hz), A_ODR_25, A_ODR_50,
// A_ODR_100, A_ODR_200, A_ODR_400, A_ODR_800, A_ODR_1600 (1600 Hz)
dof.setAccelODR(dof.A_ODR_100); // Set accelerometer update rate at 100 Hz
// Accelerometer anti-aliasing filter rate can be 50, 194, 362, or 763 Hz
// Anti-aliasing acts like a low-pass filter allowing oversampling of accelerometer and rejection of high-frequency spurious noise.
// Strategy here is to effectively oversample accelerometer at 100 Hz and use a 50 Hz anti-aliasing (low-pass) filter frequency
// to get a smooth ~150 Hz filter update rate
dof.setAccelABW(dof.A_ABW_50); // Choose lowest filter setting for low noise
// Gyro output data rates can be: 95 Hz (bandwidth 12.5 or 25 Hz), 190 Hz (bandwidth 12.5, 25, 50, or 70 Hz)
// 380 Hz (bandwidth 20, 25, 50, 100 Hz), or 760 Hz (bandwidth 30, 35, 50, 100 Hz)
dof.setGyroODR(dof.G_ODR_95_BW_125); // Set gyro update rate to 190 Hz with the smallest bandwidth for low noise
// Magnetometer output data rate can be: 3.125 (ODR_3125), 6.25 (ODR_625), 12.5 (ODR_125), 25, 50, or 100 Hz
dof.setMagODR(dof.M_ODR_100); // Set magnetometer to update every 80 ms
// begin() returns a 16-bit value which includes both the gyro
// and accelerometers WHO_AM_I response. You can check this to
// make sure communication was successful.
Serial.print(“LSM9DS0 WHO_AM_I’s returned: 0x”);
Serial.println(status, HEX);
Serial.println(“Should be 0x49D4”);
Serial.println();
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print(“Connecting to “);
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
digitalWrite(0, LOW);
delay(10);
digitalWrite(0, HIGH);
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.println(“WiFi connected”);
}
void loop() {
sendCount ++;
frameCount++;
if(frameCount < 2){
digitalWrite(2, LOW); //blue LED on
} else {
digitalWrite(2, HIGH);
}
if(frameCount > 500){
frameCount = 0;
}
if (sendCount > 1000)
{
getGyro(); // Print “G: gx, gy, gz”
getAccel(); // Print “A: ax, ay, az”
getMag(); // Print “M: mx, my, mz”
sendBundleViaOSC();
}
}
void sendViaOSC() {
OSCMessage msg(“/esp/magX”);
msg.add(mX);
msg.add(“/esp/magY”);
msg.add(mY);
msg.add(“/esp/magZ”);
msg.add(mZ);
Udp.beginPacket(outIp, outPort);
msg.send(Udp);
Udp.endPacket();
msg.empty();
sendCount = 0;
}
void sendBundleViaOSC() {
OSCBundle bndl;
bndl.add(“/esp/magX”).add(mX);
bndl.add(“/esp/magY”).add(mY);
bndl.add(“/esp/magZ”).add(mZ);
bndl.add(“/esp/accelX”).add(aX);
bndl.add(“/esp/accelY”).add(aY);
bndl.add(“/esp/accelZ”).add(aZ);
bndl.add(“/esp/gyroX”).add(gX);
bndl.add(“/esp/gyroY”).add(gY);
bndl.add(“/esp/gyroZ”).add(gZ);
Udp.beginPacket(outIp, outPort);
bndl.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
bndl.empty(); // empty the bundle to free room for a new one
// Serial.println(mX);
}
void getMag()
{
// To read from the magnetometer, you must first call the
// readMag() function. When this exits, it’ll update the
// mx, my, and mz variables with the most current data.
dof.readMag();
// Now we can use the mx, my, and mz variables as we please.
// Either print them as raw ADC values, or calculated in Gauss.
mX = dof.calcMag(dof.mx);
mY = dof.calcMag(dof.my);
mZ = dof.calcMag(dof.mz);
}
void getGyro()
{
// To read from the gyroscope, you must first call the
// readGyro() function. When this exits, it’ll update the
// gx, gy, and gz variables with the most current data.
dof.readGyro();
gX = dof.calcGyro(dof.gx);
gY = dof.calcGyro(dof.gy);
gZ = dof.calcGyro(dof.gz);
}
void getAccel()
{
// To read from the accelerometer, you must first call the
// readAccel() function. When this exits, it’ll update the
// ax, ay, and az variables with the most current data.
dof.readAccel();
// If you want to print calculated values, you can use the
// calcAccel helper function to convert a raw ADC value to
// g’s. Give the function the value that you want to convert.
aX = dof.calcAccel(dof.ax);
aY = dof.calcAccel(dof.ay);
aZ = dof.calcAccel(dof.az);
}
[/code]
Leave a Reply