Project Mozart2: How to make a music synthesizer out of your Raspberry Pi and Atmega32

October 18, 2014 Leave a comment

This is a nice and simple project for all those who has their interest at the intersection of electronics, computers, and music. I will show how I made a musical keyboard. As the project uses Jack (Jack Audio Connection Kit) to finally create the Music, it will be very easy to extend this project for any kind of sound application that you can think of.

What you need are:

  1. an Atmega32 or any similar microcontroller that has got 12 or more GPIO pins and a UART.
  2. a Raspberry Pi with some form of Linux. (A linux PC with a serial port also will work!)
  3. 2 1k resistor to make the level shifter circuit
  4. Something that looks like a piano keyboard. (Synthesizer needs to know of the pitch you are interested in playing!). If you don’t have one, probably you have to make some kind switch matrix, with switches arranged in a line

Top level design

Inside the Mega32

We use 12 GPIO pins of the Mega32 in a 4×8 arrangement to create a 32 key key-matrix, as shown in picture. The 4 lines on the left are configured to output and are activated sequentially one after the other. At any given time only one line will be active. The other 8 GPIOs of the controller are configured as input, and they will be active or inactive depending on what all keys of the keyboard are pressed. The firmware will keep repeating the pattern on the GPIO outputs, and keep reading the input pins repeatedly, and very fast. The firmware keeps on checking checking changes in keys from scan to scan. Once it detects a key press or a key release, it formats a 4byte frame containing bytes indicating

  1. frame start indicator byte
  2. Key press/release indicator byte
  3. the pitch indicator
  4. the amplitude/key velocity indicator (hard-coded to 64)

The firmware then sends the frame out to the Raspberry Pi through the UART port.

The key matrix

The key matrix as originally used looks like in picture

The switch matrix

The switch matrix

However this kind of switch matrix has a problem known as phantom-key/Ghost key. This is felt like if you press certain pairs of keys, the mega32 controller will think that two other keys(ghost keys) are also pressed.

You can go to for a nice explanation, and a solution for this. The solution is to add one diode each in series with each of the switch.

UART and Level Shifter

We use only the Tx port of the Atmega32 and the Rx port of Raspberry Pi, with a baud rate of 115200. I was running the atmega32 at 5V. But the Raspberry Pi IOs are only 3.3V capable. So a level shifter was needed. If you can run your atmega off a 3.3V supply, you can avoid the level shifter. I just used a resistive divided(don’t remember the exact values — it doesn’t really matter). Something made of a 2k and 1k resistor should work, I think.

The Raspberry Pi

The ‘MIDI encoder’ is the only block to be implemented in the Raspberry Pi side. Rest everything is off the shelf. We first configure the UART port and then keep reading the data out of UART from file /dev/ttyAMA0. Once we get a full frame from mega23, we re-encode it to MIDI format as accepted by JACK. The software then calls a Jack API to transfer the MIDI data to Jack. Jack then transports it to synthesizer(I used Hexter). Synthesizer converts MIDI commands to PCM audio, and gives back to Jack. Jack sends it to the Linux Audio stack, and it gets converted to an analog signal at the audio jack. You can connect an active speaker/headphone to hear the music.

Here are a few pictures of my prototype

The key-matrix, the level shifter(on breadboard) and the mega32

The key-matrix, the level shifter(on breadboard) and the mega32

Level shifter, Mega32, and the Raspberry Pi

Level shifter, Mega32, and the Raspberry Pi

The code at raspberry side (including a code::blocks project file) is available at: The serial format is described in the README file.


Looking back

The second one-third of life; one with a different set of responsibilities and difficulties. To me it feels like the first one-third passed by too fast. More so will be the case with the second third; as in the corporate world, we have only 8-10hrs less in our personal life; and that too – highly fragmented!

Life is too short. Before we realise, we all get old. Looking back, the only significant things probably will be the dreams that we have pursued..

Categories: Philosophy

‘The Social Network’ – A Conspiracy Theory

September 29, 2012 Leave a comment
The Fall of Mankind. - photo by iMorpheus on Flic

The Fall of Mankind.” – photo by iMorpheus on Flickr – Creative Commons

People say that “necessity is the mother of invention”. But there are many inventions that are classified as ‘out of accident’. This the story I too used to believe, until I saw it otherwise.. Necessity is the mother of all the inventions in the human history.

There used to be two types of kids.. The ones who were introverts and the extroverts. Introverts were mostly quite people who mostly became scientists, engineers, artists and the like. Extroverts on the other hand used to be very social and worried about everyone’s problem. They were the people who were in every became the politicians, the lawyers, etc.. The introvert kids used to get a lot of ridicule for being introverted, mostly in comparison with the extrovert kids.

These kids had to suffer this kind of ridicule for many generations.. For some reason the introverts decided to unite and find and end to this never-ending suffering. After many years of research they came to the conclusion that they can put and end to the suffering only if there is no single extrovert remaining in the world. This study is the founding stone of psychology.

So they came up with detailed road-map to convert every extrovert into an introvert. But the final plan would take may generation to realize.. They began thinking about speeding up the process. They came up with the Idea of Industrialization, and Economy, in which they uses the force of extroverts also to advance the sciences, technologies, and arts. This inventions I would mark as the ‘rise of the Introverts’. They made their days more productive by inventing technologies like the automobile.

Along the process, as more and more kids got exposure to the sciences and technologies, the population of extroverts began to shrink slowly. This accelerated the pace of introverts reaching their final goal. But to compensate of the shrink in extrovert population, introvert kids invented mechanisms like the robots and the computers.

All these inventions were geared up towards one invention – ‘The Social Network’. For that they had to make the computers friendly to the extrovert people. Introverts liked the machines at its original form itself. But extrovert kids never wanted to use the complex machine. Introvert kids employed the extrovert kids to make the machine ‘simple’. Even today, the introverted kids don’t like the so-called ‘simple’ machines. For them they appeared too complex. They prefer the old version of the machine that uses a keyboard.

After many iterations of the computer, they began to see the widespread usage of the machine among the extrovert kids as well. Then they introduced their final goal.. ‘The Social Network’. This unique invention gives extroverts the feeling they used to get when interacting with people, without ever leaving their computer.

The extrovert kids didn’t accept a few of the initial versions saying it is too complex. After a comparatively less number of iteration than the computers, the introverts could make the social network sufficiently user friendly to get accepted by 90% of the extrovert population. The story continues today.. Researches confirmed the generations old assumptions of extroverts to be still true, and that through the social network, they could convert the extroverts get more ridicule than the introverts..

But there is a saying among the introvert kids that the final 10% will take up 90% of the effort. Therefore the battle is still on..Extroverts will find social networks more and more addictive, where they will post every move of them, and increasingly unsocial, making their titles incorrect..

Two questions for better living

August 28, 2012 Leave a comment

I see many facebook posts showing the frustration of people towards the social, political and economic system, and the decision makers. Getting frustrated or angry is of no use in the long run. Agitations triggered by feeling at a thoughtless second in the life won’t change the system for good. When I feel like thinking that somebody is under performing, I ask myself the following two questions.
1. Could I have done better if I were at the exact same situation?
2. If yes, then why not?

99% of the time, my answer to the first question itself will be a ‘no’ or ‘probably no’. If you can’t do it, never expect somebody to do it.

Categories: Viewpoints Tags:

System C on Ubuntu – Installation

August 15, 2012 2 comments
Installation of systemc-2.3.0.tgz is straight-forward.. Just follow the instructions in INSTALL file. This log is for systemc-2.2.0.tgz

I am looking towards learning systemC. So the first step is setting up one on my PC. It was not so straight-forward for somebody like me who has done only a few hours of research about systemC. So I thought of logging my experience setting up systemC in Ubuntu 11.04. (The best way to backup information is to make it public)

1.What is it

Initially I was thinking it is a new language all-together, with a different set of tool-chain. With a little bit of research, I came to the conclusion – “to the user, systemC is c++ library and header file collection, that can be linked to other c++ files using any c++ compiler & linker”.

2. Where to get it

According to the wikipedia page on systemC, it’s just an IEEE standard, which anybody can implement. So there will be multiple versions of the systemC. Luckily, Accellera has a proof of concept implementation, which is available for free from here. You need to register, providing your email id. After registration and logging in, the systemc-2.2.0.tgz (I am not very clear about the TLM stuff. Will first experiment with this and then the latest 2.3.0, with TLM)can be downloaded. If you directly go for systemc-2.3.0.tgz, some of the steps here may be irrelevant.

3. How to setup

Unzipped the file, and read the README and INSTALL. Followed the instructions in INTALL file. But got a compiler  error in sc_utils_ids.cpp, regarding some namespace issue while running make. As an embedded engineer, I am really bad at any language other than plain C. So did the searching, the following two links came up



Downloaded the 0001-missing-system-headers-function-qualification.patch file from the second link. Patched the original sc_utils_ids.cpp file with the patch using command

patch -i ~/Downloads/0001-missing-system-headers-function-qualification.patch ../src/sysc/utils/sc_utils_ids.cpp

Then did a make and make install again. This gave me the libraries and include files in /usr/local/systemc-2.2.0 (As I have used the ../configure –prefix=/usr/local/systemc-2.2.0 to do the configuration).

Categories: Log Tags: ,

Are you blocking your way? I did a comparison between the present me, and the me 6 years back, when I was beginning as an Engineer. What I found is that the rate of progress that I made at that time is substantially larger than the one I am making now-a-days. I couldn’t find any external reason. The only possibility is that my own thoughts and believes are causing the drag.

Six years back, my attitude was like, ‘I have nothing I can be proud of; I have nothing to loose’. But these days it seems to me that I am too conscious about the way different people perceive me. This was too big a revelation to me. I felt that I am more complex than I ever imagined (One of my guiding philosophies is ‘Sense and Simplicity’. I always wanted me and thought myself to be simple.).

There is a feeling like ‘I have a reputation, and I don’t want to spoil it’ running in the sub-conscious. This I felt is the reason why I am more conscious about what people perceive me as, rather than what I really am. This is the same thing that is stopping me from doing many things (may a-little-bit-risky things). This precisely should be the system that is causing all the drag that I am experiencing.

So what’s the way forward?. Destroy this ‘ego’. More obscurely as ‘March in the path of self destruction’ (not ‘self-destruction’; the hyphen changes the whole meaning by 180 degree). Involve in things that can really hamper your public image, and let your image be gone. Fear of height comes when you look downwards. Make yourself believe that you have nothing to loose, no social reputation, and always look up. This is the way you can overcome the fear of height, even if you are at a height.

Do you know what the best condition a man can ever get? To have a bunch of people around, who doesn’t give you any respect. This I would call as ‘Impedence matched’. Some call as ‘wavelength matched’ or ‘same frequency’. This bunch of people will look at each other, and they grow together. There is minimal drag.

Thus I am in the path of a bit transformation. Don’t know if the mission will be scrapped before the completion. But anyways, it has already begun. Let me see if I can ‘Destroy the temple and rebuild it in three days’ (Well.., I think it will take me more than even three months – but lets not alter the beauty of that quote..).

Image under creative commons license –

Must use tools for Electronics/Electrical Students

ToolsHere is a list of computer based tools, which i know or have used and felt to be great and must-use for every elctrical/electronics engineering students. Priorities are price, ease of use on multiple platforms, popularity etc..

Analog/Power Electronic Circuts – LTspice (works smoothly in wine)
Antenna Design – MMANA-GAL
C/C++ IDE – Code::Blocks (linux & win versions available. use the installer with mingw, if you want compilers also installed)
Embedded Development – SDCC (8051), WinAvr (AVR)
Networking – WireShark (have both linux and win versions)
Numerical Simulations, Visualisation – Octave (great on linux, with QtOctave), Matlab (non free), Scilab. Scilab’s language is a bit different from the other two.

There are many other tools like KiCad that are worth giving a try. For digital circuit simulation, evaluation/student version of many softwares are available. In addition to these, I have used tools for PCB layout etc, which I don’t remember the name. is a good place to search for authentic free softwares.