The headphone jack meets an actual spark gap!

So, we had the opportunity to test out the headphone jack with an actual spark gap and it was absolutely wonderful. Check it out:

With the data we can actualy find out the frequency of the spark occurrence. In our case it turned out to be ~ 34 – 36 Hz. And since this is in the Audible range we can actually hear this (somehow we missed this when making the video)

Screenshot from 2017-06-10 20:01:07

Audio file : GoogleDrive


Part – I – Lightning detector with a simple headphone jack

Part – II – Detecting switching ON/OFF of Tube Light using headphone jack

Part – III – Cigarette lighter spark detection using headphone jack


It is ridiculously easy to generate any audio signal using Python

Updated: May 15,2019

Now it comes as a surprise to many people when I tell them that generating an audio waveform is extremely simple.

One needs to have basic understanding on how audio signals work and basic python programming to generate any audio wave form. This post will show you exactly how.

Python packages needed: Numpy, Scipy


How to play the audio the generated audio file on computer ?

1. Command line using SoX

play -t raw -r 44.1k -e signed -b 8 -c 1 test.wav

where -r = sampling rate -b = sampling precision (bits) -c = number of channels

2. Use Audacity (check video)


Link to code : GitHub

You can find a list of other waveforms that can be generated using SciPy here

Known Issues:

[1] This does add any headers to the audio file and therefore you cannot play it on any media player as is . Check this reddit post if you really want to have one.

[2] Adding headers to the above code seems to be making it slower. And this is a problem if you want to make larger audio files.

[3] The code generates only 8-bit audio signal. Feel free to play around with the code to change it to other formats.

[4] A lot of technical details were conveniently not included in code in order to appeal to the theme of this post. And therefore this code is not “efficient”.

Detect switching ON/OFF of Tube-light with a simple headphone jack

Now in our previous post, we established that the headphone jack can be used to successfully detect electromagnetic disturbances ( Sparks and Lightning ). In this post, we explore the interaction of the tube light with the headphone jack.


Here is a close up of the peak that you saw in the video:


Nowadays of course, we have moved on Non-flickering tubelights and it is highly unlikely that we might be able to detect the peculiar peaks that you see on this. But to be sure we are currently working on this and we will keep you guys posted on the results.

Also if we could incorporate this into a IoT network, things will go crazy!!!


*   Image Source

Testing basic headphone jack functions (Android)

In this post we will be trying to emulate basic functions as prescribed in the Android Documentation.  In order to understand the functions we first need to understand the circuit layout of the headphone jack which is as follows:

Screenshot from 2017-05-22 11:11:21

Now with this circuit in hand you can perform a series of tasks. This has been summarized in the table below.

Screenshot from 2017-05-22 11:03:00

This post will primarily focus on the Function A since that is the one that is commonplace in all mobile phones with a headphone jack. In the following video, we demonstrate how to emulate the following functions using a headphone jack

  • Play/Pause
  • Open Google Voice
  • Next song
  • Radio
  • Emulating Google voice

Every speaker is also a microphone!

Sometimes it comes as a surprise as a people to people when I tell them that every speaker is also a microphone. This is true because in a speaker you send in electrical signals to change the way a speaker cone moves. This in turn produces various sounds.


With the same setup, if one provides a mechanical vibration to the diaphragm, this will generate audio signals corresponding to that mechanical vibration.



We have made a video demonstrating this using a headphone jack and a phone. In order to loop the sound from the mic to the speaker in the video, we use the following command on Linux:

pactl load-module module-loopback latency_msec=1

LiFi using IR sensor and headphone jack

In our previous post, we explored using the Solar panel + Led as a LiFi module. In this post, we shall be using IR sensors in order to transmit data (tones and square wave signals) wireless. The schematics of the setup are given below:


One can use this to send square wave of any frequency independently as well. This has been demonstrated in the video below

Video Demo:
Watch the video demo for a live demonstration of the working of this concept.

This is not limited to tones, one can use it to transmit raw music as well.


Exploring /dev/input

Linux is one of those operating systems in which once you know your way around, you are unstoppable. In this post, we will explore what you can possibly do with access to /dev/input directory. Now you typically need to have superuser privileges to access this directory but there is a way around it. ( check this stackexchange )

The /dev directory

This is the directory in Linux that contains all the device files for all the devices that are on your system.

/dev/input – The input is a subdirectory that holds the device files for various input devices such as mouse, keyboard, joystick and so on.

Screenshot from 2017-04-18 22:10:46

This is a screenshot of my input directory and as you can see there is the mice/mouse0/mouse1 which are files corresponding to the touchpad/wired mouse/wireless mouse respectively. And then you have these ‘eventX’ files. Yours might look similar but depending on the nature of your device you might have more or less options available.


With so many event files, how can one possibly know which one corresponds to which ? This script is what you need:

cat /proc/bus/input/devices

This script would display relevant information about all the input devices connected to your system. Here’s a sample output for the Lid switch :

Screenshot from 2017-04-18 13:04:47

Now what we want is the event handler number for the lid switch which we can see from the information as event0. And through this one can map all the events with their corresponding devices.

Alternative method:

An alternate approach to doing this is to look at /dev/input/by-id or /dev/input/by-path, they have symbolic links to /dev/input/event<x>. But the only downside to this is that it does not display all the possible devices. We will discuss how to read from both in this post.

Let’s read the data stream !

Say you want to read the mouse file, you only need to open the file using ‘cat’ and start moving the mouse.

(sudo) cat /dev/input/event5

Screenshot from 2017-04-18 22:57:25

You would observe that as you are moving the mouse a stream of data is being dumped onto your screen. This data although seems crazy contains the information for the movement of the mouse.

Interpreting the input stream

The format for the input stream is given in the Linux documentation as follows:

struct input_event {
	struct timeval time;
	unsigned short type;
	unsigned short code;
	unsigned int value;

struct timeval – 16

unsigned short – 2 ( x 2 )

unsigned int – 4

Total size = 24 bytes

What this means is the first 16 bytes of data contain the system time, which in our case is not essential. The rest 8 bytes of data contain the actual relevant values. This is the output format of all eventX files.

Reading the mouse

Reading the mouse is probably the simplest to start with because one does not need to go through the trouble of accessing any eventX files. The mouse file in the /dev/input directory outputs a stream of 3/4 bytes ( Button value, x , y ).

You would get a 4 byte stream if the mouse is configured with the scroll wheel. Unfortunately in my case, even though I use a mouse with a scroll wheel, it was configured as a PS2 mouse. ( If you are in a similar situation – read this)


Map of the (x, y) data with the corresponding directions

My touchpad and wireless mouse do not have any special buttons and therefore when the button values are mapped with the corresponding clicks, I got the following :

Left_Button – 9

Right_Button – 10

Scroll_Button – 12

When I was going through this stackexchange, the code seemed to suggest different values for the each of these buttons.

Python Code for reading mouse:

import struct 
f = open( "/dev/input/mice", "rb" ); 
# Open the file in the read-binary mode
while 1:
  data =  # Reads the 3 bytes 
  print struct.unpack('3b',data)  #Unpacks the bytes to integers

Video Demo:

Reading the keyboard

Much like the mouse, one can opt for an easier way by just reading from the /dev/input/by-id or /dev/input/by-path, but for the love of it let’s try to play around with eventX files. Let’s go about this step by step.

  1.  Find the event handler number for the keyboard ( Mine was event5 )
  2.  Reading the byte data and converting it to the format specified in the Linux Documentation using the struct module in python
  3.  Extracting the relevant information from the converted data.


Python code for reading keyboard:

import struct 
f = open( "/dev/input/event4", "rb" ); # Open the file in the read-binary mode
while 1:
  data =
  print struct.unpack('4IHHI',data)
  ###### PRINT FORMAL = ( Time Stamp_INT , 0 , Time Stamp_DEC , 0 , 
  ######   type , code ( key pressed ) , value (press/release) )

Video Demo:

Other eventX ?

What should you do if you would like to read from another eventX file ? Well, from my experience I can share with you this. : The first 16 bytes in the file are always almost the time stamp and you can read only that data by:


The rest depends on the type of output which you can find out by using the evtest program.

sudo evtest /dev/input/eventX


** Now in theory, using uinput one can reverse engineer to emulate events. But we are still currently working on that. If you know anything about this, please ping us at