Articles Hierarchy

Articles Home » Arduino Projects » Ardui NO midi

Ardui NO midi

i read that the Arduino UNO can be a MIDI device?
would be nice because i need one to test my RPI sound project. python / sound / midi
i started to play ( on arduino ) with some code for capacitive sensor,
but i should have checked on that first
and the construct of the 3 byte MIDI data structure, but see that first i needed some reading to understand the difference in the concepts:

1. it is said that arduino can be programmed using the serial Baudrate MIDI needs
31250 baud. The format is 1 start bit, 8 data, no parity, 1 stop bit.
(even the IDE terminal can not work on that baud rate)

2. many examples relate to use pin 1 ( tty of USB ) or pin 3 ( softserial )
and connect a resistor and a MIDI connector.
means a original MIDI device is to be connected.

3. but for using USB there are some ways,
-3.1- is you talk usual ARDUINO serial and run processing, home made software or MIDI serial converter
( for windows MIDI YOKE NT ) ( for RPI ? ttymidi ? )
Link , Link , Link

-3.2- use a special system software on the arduino, what makes it a real MIDI USB device
recognized by the PC called "HIDUINO_MIDI firmware"
for this / and after this / you can talk to it only by ISP programmer; its not a arduino anymore.
Link , Link

if i want explore that i would need a programmer ( can use a other arduino ). But as the USB chip is involved, could loose the board. I only have one ARDUINO UNO. ( should buy 2 china copies to play with that! ) ( spare 328 cpu i would have, but that's not the point here )

-3.3- use a USB MIDI converter "hardware"
Link , Link , Link

here i would need shopping, i found one in my town in a computer center, but she wanted 17 US$ instead of 7, NAAA, no need.


But if i want explore way 3.1 i would try to make a python software on the RPI, but for this it would be good to know if python pygame can be a virtual MIDI device.
I not understand if that does it. But its worth a try, so back to RPI PYTHON

HURAY that works perfect
a very nice python pygame MIDI keyboard, i connected to FLUID SYNTH via aconnect;
with that as example ( without the keyboard..) i can start to make my own connector software
ARDUINO USB to RPI MIDI DEVICE.


on the Arduino side i start with a loop:
- i have no time to play music, need time to write the code, so set autoplay:
NOTE ON , wait 950, NOTE OFF, wait 50, for random Notes between 50 ..80. ( of 0 .. 127 )
Velocity fix 127 ( of 0 .. 127 )
as i write both sides, the arduino sketch and the "RPI python pygame MIDI device" i am actually free regarding the protocol, but i want be close to MIDI as possible.
But as i see that MIDI sends 3 bytes ... no delimiter i think for me more easy to send / catch lines.
So i Serial.write Status byte, Note byte, Velo Byte and a Serial.println(), using a speed of 115200 Bd.
and in python expect lines with 3 bytes ( netto content ) to interpret as MIDI, or print them to terminal,
with that debug the arduino is very easy, if enabled just print long ( >3 chars ) lines, this would be impossible with real MIDI.

to calculate the midi commands ( first Status byte ) i use
basis = 127
channel = 1 ..16
NoteOFF = basis + channel
NoteON = basis + 16 + channel
( often i see instead of NoteOFF, a second NoteON ( Velo 0 ) )
other commands:
polyphonic after touch
PAT = basis + 32 + channel
control mode change
CMC = basis + 48 + channel
program change request ( PCR , instrument , 0 )
PCR = basis + 64 + channel = 192 ( channel 1 )
channel after touch ( CAT, pressure,0)
CAT = basis + 80 + channel
pitch bend change (PBC, LSB (0), MSB )
PBC = basis + 96 + channel

As i already have 2 inputs for capacitive inputs ( touch wire and a resistor ) so i can change 2 parameter,
-1- close /touch A wire AON, ( set channel from 1 to x ) ( ref. fluidsyth init file )
-2- close / touch B wire BON, ( set instrument from 0 to y )
sadly the switch back logic touch A and B reset both AOFF BOFF until now not work ( hardware level influence each other ).

On the RPI side i use a pygame window ( but can be disabled USE_GUI ) ( and USE_MIDI can disable the pygame midi part for diagnostic )
the program can be started from command line using a default instrument ( for channel 1 ) by " -i xx"
if the arduino is connected the serial.Serial(port,115200,20) should work. ( a port list is shown in terminal )
and then it runs only inside the "read_USB()" function.
as event only pygame.QUIT ( window x click ) is used
the MIDI decoding until now only
- Note ON / OFF and channel, Note, Velo
- the program change request to pygame midi set_instrument.
as the channel thinking is 1 .. 16 ( basis 127) for pygame midi_out use ( channel - 1 )

at program start a test note play helps to get fluidsyth and aconnect tested, even without arduino.

once the fluidsynth server runs ./start_fluidsynth.sh
aconnect 14:0 128:0 ok, check with aconnect -i -o -l
you can start and stop the midi program without problem. using keyboard AND USB MIDI might be tricky...


play only with one cap sensor and use it for change from random notes
to pitch bend control by sensor reading works from communication, midi...
but not too much from sensor, looks like when the timing / sample rate changes the readings change?? then the sensor does what it wants, even without touch...
and after some time it recovers ( that internal auto calibrate function brings the readings down, below my limiter, and program change back from pitch control to random note play )
well, back to hardware mode, other sensors...
anyhow i had fun, but the cat did not like it!



just to show here as example, i cut out the essence of the python and arduino script:

code python2 and code arduino and sound


looks like piezos are good sensors for arduino, see also here connected to a analog in with a 1Mohm parallel for reduce V / protect arduino input ( actually a 5.1V Z diode even better). also might be a chance to get a stroke / velocity information ( analog in ) but we know analog sampling is kind of slow. but as much i like to play with it, i not have even one available for testing.
Today i see a new shop, just a sign LED..., and inside it looks like electronic...
the boys not talk english, but a old lady behind the counter understand my "piezo"
open the ONE drawer and give me one of that disks, it looks old, damaged, and the not insulated wire easy could produce a short?
we play with the ohm meter, but i think we both did not know what to expect, and i see nothing.
i pay 10THB and now check at home: ( i cover the disk with a isolating rubber / not touch it directly )
ohm: open, on pressure down to 6Mohm.
Dc volt: 0 on pressure up to 20V with the back ( metal ) is plus, but i see that all other piezo pictures show a black wire from the metal back! ( speaker thinking? )
reading i can use it as sensor and as speaker
ok, a digital output of 5VDC via D9 not give much sound, tested with 2 piezos now, a new, smaller with wires for 12THB, even less sound.
UNTIL i build a speaker: here i use a Yoghurt cup and guess what my cats eat today? tuna from a can!
a rubber press the piezo against the back of the can, some isolating material put the force in the sensor but protect it also. i think for a real speaker i just have to glue it on the can floor, in or outside.

here a question for the scientists among you: whats the basic difference between the cup and the can?
yes, the smell! just joking, the can has a floor like a membrane, ( production reasons? cooking pressure? ) while that Yoghurt cup is very stiff. for the piezo mounting i test a easy clamp way.

i see a blog where a auto transformer was used to transform the arduino pulses to a higher voltage for the piezo, pls see here
Now about the testing code, starting from original Example 1: Play Melody but put in also some piano and MIDI features. Connecting USB to RPI and start fluidsynth and my serial_midi_device.py i can play with new piezo speaker and MIDI RPI...

arduino sketch
ok, so much for piezo as speaker, now:


back to piezo sensor force or touch...
we need to read the analog values first to get a idea about the range,
i use the PMS3.3 software ( set to 50Hz batch sampling )
and make a Virtual Zero point by
GND -- 500k -VZ- 500k -- VCC
GND -- PZO -VZ
GND -- ADC -A1
connect the piezo between that VZ and GND.
channel 0 to GND, channel 1 to VZ/piezo, channel 2 to VCC what is actually less as 5V because USB power,
but the micro pro arduino don't know this, so 5V reading is wrong.

The sensor is covered by some isolating rubber, so my static charged fingers not influence the reading.

but as it was not easy to catch the press event i started with a new version,
a main program read Ain ( example every 0.25sec // 4 Hz ) if value outside 512 +- trigger (like 10)
start a batch read 38 samples in 0.42 sec ( about 90Hz)

with adjusting the batchtime ( batch samples * batch sample time ) also do a debouncing.
the analysis of the batch values ( like array min and array max val // or even calc RMS) can be used as stroke / velo info.
arduino sketch
bad sadly i have to mention, that when i remove the piezo and touch the VZ with the finger, i have a bigger signal.
( as you have if you touch a open Ain with the finger you see a 50Hz sinus )
the reason is that the VZ is not strong! for a arduino Ain that VZ bridge by two 500kohm is near to open.
so i repeat the test with the original design without VZ, Ain -- piezo || 1M -- GND and a trigger at 100.



In an other blog i read about using that electric foam ( used for transport IC protecting them from static charges ) but must test if can read any analog signal from that.