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

Screenshot_2019-05-15_11-06-02

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”.

DIY: Obstacle detector with Audible feedback (IR sensor + Headphone jack + 555 timer)

We were inspired by the buzzer that you find in mobile and laptop showrooms – the ones that produce this annoying high frequency tone if you fiddle a ‘little too much’ with the displayed product.

schematic

We use a 555 timer in its Astable mode to produce the frequency tone and couple it with a digital IR sensor module. We do this by connecting the output pin and the ground parallel to R2 in the figure.

single-ir-sensor

And as a result when there are no objects in the vicinity, the system produces a high frequency tone, but when an object is introduced the sound dies out.  This is attributed to the change in resistance value.

Here is another variation of the same:

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.

eventX

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)

mouse_usb

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 = f.read(3)  # 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 = f.read(24)
  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:

struct.unpack('4I',data[:16])

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 153armstrong@gmail.com