Photoresistor Volume Control 1


Well here we go with my first official blog post since deciding to start blogging more.  This project came about one day when I was bored and was thinking of some things I could do with some of the parts I had laying around.  I originally started out using the Arduino UNO to get my feet wet with micro-controllers and since then I have mostly been working directly with the chips themselves. (ATTiny85 , and Atmega328).  Although the Arduino UNO is a good development board to test with, I feel its important to understand how to work directly with the chips themselves, so when you decide to implement them in a somewhat permanent project, that you’re not using a $30 dollar board every time.  The Atmega328 chips are around $2 or $3  and the Tinys are even cheaper ( ~$1 or cheaper).  OK, enough background info…let’s move on to the project at hand.


  • Please read this page on how to use the BusPirate to program your chip via CLI (avrdude) or GUI (Arduino IDE)


Parts Needed:

  • ATTiny85
  • BusPirate
  • LDR Photoresistor (R3)
  • 4.7KΩ Ohm Resistor (R4)
  • Breadboard
  • Jumper wires
  • Power Source (Batteries, Bench Power Supply, or Power from BusPirate via USB)

The picture above  shows 4 wires coming from the BusPirate to the ATTiny85.  Each of these colors come from the cable I purchases from SparkFun. If you decide to use the BusPirate as a power source,  reference the pin outs below to connect GND and  +5V/Vcc accordingly.  The BusPirate automatically turns on and uses its on board power supply when uploading a sketch.  You will need to manually turn the power on (from within the BusPirate menu) when using BusPirate for power and not using an external power source.  I will explain more on this later.



 Once you have followed the schematic (above) and wired everything up (making sure to use the ATTiny85 pinout and BusPirate cable color chart) it’s  time to upload the following sketch.

Since the ATTiny85 does not have built in Serial  ports, you will need to us the SoftwareSerial library and specify which ports to use for TX and RX.  I think most of the above sketch is pretty self explanatory.  The only thing really worth mentioning is the map() function.  This takes the analog range (0 – 1024) and maps it to a new range of anything you specify (up to 255).  With this being a volume control, I have made the range 0 – 100 for obvious reasons.

NOTE: Be careful not to use the PIN1 / PB5 .  If you miss-configure this pin you will brick your ATTiny and will have to do a high voltage rest of the chip.  See this page for more detail on “unbricking” your chip.

Now to verify your sketch is working you will need to make sure your TX and RX are on the correct pins ( if you used something else besides 0 and 1 listed above)  Since we are using 0 and 1 above, you do not need to move any cables.  Since MOSI  and MISO are on the TX and RX pins.  We now need to connect into our BusPirate menu and turn on UART mode so that we are able to receive the serial output from the ATTiny.  To connect to the BusPirate I use picocom and connect with 115200 baud.

To verify what serial port your Bus pirate is on, you can run this command and look for FTDI

Once connected to the BusPirate you should see the HIZ> prompt.  Press 3. for UART, Press 5 for 9600 (baud specified in sketch above),  and keep the defaults on the rest (press enter) until you get to the UART> prompt.

If you are using the BusPirate as the power source to run your circuit, this is the point where you need to turn the power supply on.  At the UART> prompt press W to turn on power and w to turn power off.


Now you have the option of permanently enabling UART mode (transparent) or putting it in a test mode (live).  I would recommend using the live mode first, and then use the transparent mode once you have verified you are receiving data correctly.  To do this you type in (0) and the prompt and press ENTER.  This brings up the macros for the UART mode. Press (2) and ENTER to use live/test mode. Once in Live mode you will have the option of exiting by pressing any key, and you should start seeing the serial output from the ATTiny.

Once you have verified the ATTiny is outputting correctly, press any key, then type (1) and press enter, then Y.  This will enable the transparent mode and will be ready for use with the bash script below.

Now for the fun part.  Taking the output value and controlling the volume of your computer with a bash script.  Please see the comments in the below code for explanation on what the script is doing.


If all went well, you should be having some fun controlling the volume on your pc by turning lights on and off and moving your hand over the LDR sensor.  If you have any questions or suggestions on how I’ve done things, please leave a comment below



Be Sociable, Share!
  • Reddit
  • Delicious
  • Google Reader
  • Digg
  • Facebook

One comment on “Photoresistor Volume Control

  1. Pingback: Community Corner: This Week in Adafruit’s Community « adafruit industries blog

Leave a Reply