Accelerometer Visualizers

Servo

Visualizing the change of accelerometer with a servo.

BOM:

Arduino Uno x 1

Accelerometer (MPU6050) x 1

Servo x 1

Jumper wires x 8

 

Schematic:

accelerometer servo

 

 

 

 

Code:

X-index turns the servo from 0 to 90 degree and y-index turns the servo from 90 to 180 degree, while z-index changes the speed of servo’s turning.

//mpu 6050 code reference http://playground.arduino.cc/Main/MPU-6050#short
#include<Wire.h>
#include <Servo.h>
const int MPU = 0x68; // I2C address of the MPU-6050
int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;
unsigned long pTime = 0;
Servo servo;
void setup() {
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
Serial.begin(9600);
servo.attach(A3);
}
void loop() {
long cTime = millis();
Wire.beginTransmission(MPU);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU, 14, true); // request a total of 14 registers
AcX = Wire.read() << 8 | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY = Wire.read() << 8 | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ = Wire.read() << 8 | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp = Wire.read() << 8 | Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX = Wire.read() << 8 | Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY = Wire.read() << 8 | Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ = Wire.read() << 8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
int posX = map(AcX,-1000,1000,0,90);
int posY = map(AcY,-1000,1000,90,180);
int posZ = map(AcZ,-1000,1000,1000,200);
Serial.print("AcX = ");
Serial.print(AcX);
Serial.print(" AcY = ");
Serial.print(AcY);
Serial.print(" AcZ = ");
Serial.print(AcZ);
Serial.println();
if(cTime – pTime >= posZ){
if(AcX>AcY) servo.write(posX);
else servo.write(posY);
pTime = cTime;
}
}

Demo Video:

Processing

Visualizing the x index, y index, and z index of the accelerometer in processing. The processing program reads the values through the serial port and display a mosaic art based on the users’s interaction with the accelerometer.

BOM:

Arduino Uno x 1

Accelerometer (MPU6050) x 1

Jumper wires x 5

 

Schematic:

processing

 

Code:

Arduino

//mpu 6050 code reference http://playground.arduino.cc/Main/MPU-6050#short
#include<Wire.h>
const int MPU = 0x68; // I2C address of the MPU-6050
int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;
void setup() {
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
Serial.begin(9600);
}
void loop() {
Wire.beginTransmission(MPU);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU, 14, true); // request a total of 14 registers
AcX = Wire.read() << 8 | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY = Wire.read() << 8 | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ = Wire.read() << 8 | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp = Wire.read() << 8 | Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX = Wire.read() << 8 | Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY = Wire.read() << 8 | Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ = Wire.read() << 8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
Serial.print(AcX);
Serial.print(",");
Serial.print(AcY);
Serial.print(",");
Serial.print(AcZ);
Serial.print(",");
}

Processing

import processing.serial.*;
Serial port;
int[] readings = new int[3];
void setup()
{
size(600, 600);
noStroke();
port = new Serial(this, "/dev/cu.usbmodem1411", 9600);
}
void draw() {
while (port.available() > 0) {
byte [] inBuffer = port.readBytes();
int limit = 10;
port.readBytesUntil(limit, inBuffer);
if (inBuffer != null) {
String str = new String(inBuffer);
String[] token = splitTokens(str, ",");
if (token.length >= 3) {
readings[0] = int(token[0]);
readings[1] = int(token[1]);
readings[2] = int(token[2]);
}
println(readings[0]);
println(readings[1]);
println(readings[2]);
}
}
float posX = map(readings[0], 200, 450, 0.2, 1.5);
float posY = map(readings[1], 200, 450, 0.3, 1.7);
float posZ = map(readings[1], 200, 450, 0.1, 1.2);
int S = (int)posZ*30;
for(int i = 0; i <15; i++)
for (int j = 0; j < i; j++) {
for (int k = 0; k < i; k++) {
fill (random(255), random(255), random(255));
noStroke();
rect(width/i * j +25, height/i * k + 25, S*posX, S*posY);
}
}
}

view raw
mosaic.pde
hosted with ❤ by GitHub

Demo Video:

 

LCD Screen

BOM:

Arduino Uno x 1

Accelerometer (MPU6050) x 1

TFT Screen x 1

Jumper wires x 8

 

Schematic:

Unfortunately Fritzing doesn’t have the lcd screen I am using.

97k66v

CS to pin 10

RST to pin 8

RS to pin 9

SDI to pin 11

CLK to pin 13

LED to pin A0

Code:

The LCD screen draws circles of different sizes and colors based on the acceleration directions. X-index = blue, y-index = red, and z-index = green. The size is based on the range of acceleration.

//mpu 6050 code reference http://playground.arduino.cc/Main/MPU-6050#short
#include<Wire.h>
#include "SPI.h"
#include "TFT_22_ILI9225.h"
#include <TFT.h>
#define TFT_RST 8
#define TFT_RS 9
#define TFT_CS 10 // SS
#define TFT_SDI 11 // MOSI
#define TFT_CLK 13 // SCK
#define TFT_LED A0
const int MPU = 0x68; // I2C address of the MPU-6050
int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;
unsigned long pTime;
int interval = 800;
TFT_22_ILI9225 tft = TFT_22_ILI9225(TFT_RST, TFT_RS, TFT_CS, TFT_LED);
void setup() {
Wire.begin();
Wire.beginTransmission(MPU);
Wire.write(0x6B); // PWR_MGMT_1 register
Wire.write(0); // set to zero (wakes up the MPU-6050)
Wire.endTransmission(true);
tft.begin();
tft.setOrientation(2); // set vertical display
Serial.begin(9600);
}
void loop() {
long cTime = millis();
Wire.beginTransmission(MPU);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU, 14, true); // request a total of 14 registers
AcX = Wire.read() << 8 | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
AcY = Wire.read() << 8 | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
AcZ = Wire.read() << 8 | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
Tmp = Wire.read() << 8 | Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
GyX = Wire.read() << 8 | Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
GyY = Wire.read() << 8 | Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
GyZ = Wire.read() << 8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
Serial.print("AcX = ");
Serial.print(AcX);
Serial.print(" AcY = ");
Serial.print(AcY);
Serial.print(" AcZ = ");
Serial.print(AcZ);
Serial.println();
int x = map(AcX, –15000, 5000, 0 , 50);
int y = map(AcY, –15000, 5000, 0, 50);
int z = map(AcZ, –1500, 5000, 0, 50);
if (cTime – pTime >= interval) {
tft.clear();
tft.setBackgroundColor(COLOR_BLACK);
int Size;
int rgb;
if(AcX > AcZ){
Size = x;
rgb = COLOR_GREEN;
if(AcZ > AcY){
Size = z;
rgb = COLOR_RED;
if(AcY > AcX) {
Size = y;
rgb = COLOR_BLUE;
}
}
}
tft.fillCircle(80,100,Size,rgb);
pTime = cTime;
}
}

 

Demo Video:

Leave a reply

Skip to toolbar