Python Script

Author Gravatar Image MIKEY SKLAR
This 100 line python script can be pasted into a editor and saved on your raspberry pi. You can also grab it directly from the pi if its connected to the Internet by running git clone git://

The script is fairly simple. Half of the code (the readadc function) is a function that will 'talk' to the MCP3008 chip using four digital pins to 'bit bang' the SPI interface (this is because not all Raspberry Pi's have the hardware SPI function)

The MCP3008 is a 10-bit ADC. That means it will read a value from 0 to 1023 (2^^10 = 1024 values) where 0 is the same and 'ground' and '1023' is the same as '3.3 volts'.  We don't convert the number to voltage although its easy to do that by multiplying the number by (3.3 / 1023).

We check to see if the pot was turned more than 5 counts - this keeps us from being too 'jittery' and resetting the volume too often. The raw analog count number is then converted into a volume percentage of 0%-100%. When the trimpot is turned up or down it will print the volume level to STDOUT and adjust the audio level of the playing file by telling the mixer to adjust the volume.
After you have pasted this script into a file. Make it executable. 
Copy Code
chmod +x
Last updated on 2015-02-28 at 11.27.23 AM Published on 2012-07-29 at 03.58.38 PM