Assignment #9
Input Devices
The ninth Fab Academy assignment is about input devices. We have to measure something using sensors and a microcontroller.
1 – the sensor
Because lab access is still restricted during the coronavirus pandemic, I’ll use an Arduino Nano. The sensor is an MPU-6050 on a GY-521 module. I plan to use it in my final project to detect the MIDI controller’s tilt. To guide the process, I followed this tutorial
2 – The code
The code is a little more complex than in the embedded programming assignment. I used the Wire library and functions that convert sensor readings into angles, plus mapping functions to make the values easier to work with. Once the code was ready, I uploaded it through the Arduino IDE:
#include
#include
#define MPU 0x68 // I2C address of the MPU-6050
double AcX,AcY,AcZ;
int Pitch, Roll;
void setup(){
Serial.begin(9600);
init_MPU(); // Inizializzazione MPU6050
}
void loop()
{
FunctionsMPU(); // Acquisisco assi AcX, AcY, AcZ.
Roll = FunctionsPitchRoll(AcX, AcY, AcZ); //Calcolo angolo Roll
Pitch = FunctionsPitchRoll(AcY, AcX, AcZ); //Calcolo angolo Pitch
int xposition = map(Roll, -90, 90, 0, 179);
int yposition = map(Pitch, -90, 90, 179, 0);
Serial.print("Y: "); Serial.print(yposition);
Serial.print("\t");
Serial.print("X: "); Serial.print(xposition);
Serial.print("\n");
}
void init_MPU(){
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);
delay(1000);
}
//Function to calculate angles
double FunctionsPitchRoll(double A, double B, double C){
double DatoA, DatoB, Value;
DatoA = A;
DatoB = (B*B) + (C*C);
DatoB = sqrt(DatoB);
Value = atan2(DatoA, DatoB);
Value = Value * 180/3.14;
return (int)Value;
}
//Function to get X,Y,Z from MPU6050
void FunctionsMPU(){
Wire.beginTransmission(MPU);
Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU,6,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(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
}
3 - testing the code
I opened the serial monitor to view the sensor readings. The code worked, and the device continuously reported its angle relative to the X and Y axes.
5 – test with the final project top shell
I then attached the GY-521 module to the top shell of my MIDI controller to check the fit and appearance. I liked being able to see its LEDs from outside, and the sensor still worked well.
6 – MUX and buttons
My final project needs 12 buttons to trigger notes, so I’ll also use a multiplexer, or MUX.
A multiplexer lets several inputs, eight in this case, share a single signal pin.
I connected eight switches through the multiplexer and four more directly to the controller, giving me 12 inputs in total.
7 – Coding the MIDI device
To further test the success of this operation(s) I went straight to the MIDI library , assigned a MIDI note to each button, and mapped the gyroscope data to four MIDI Control Change values. These can control effects, volume, and other musical parameters. To check the output, I used software called Hairless . Hairless is a virtual MIDI machine that takes data from a serial port and turns it into proper MIDI data. After a few tweaks and changes in my code I managed to send good MIDI signals to Hairless!
8 – Designing a board for the input device
To complete the assignment, I’ll design a PCB for the input devices tested this week. The pandemic prevents me from making the board, but my global evaluator suggested designing it to improve my PCB skills. Inspired by the Satshakit , I studied the ATmega328P datasheet and pinout, worked out the connections, and drew the circuit in KiCad.
I then moved to the PCB editor and routed the connections. There were too many components to fit every track on one side, so I added vias and moved some tracks to the bottom layer.
Vias are useful, but I tried to minimise their number to keep future milling manageable. I used KiCad’s 3D viewer to preview the board.
Source files
Here are all the source files for the Input Devices assignment:
1 - code with gyro + MIDI
2 - code with gyro + buttons(MUX) + MIDI
2 - KiCad file of the board
