Dr. Scott M. Baker

Auto-dimming Chumby hack

by admin on Apr.22, 2011, under Electronics Projects

I love my Chumby linux-enabled alarm clock, but I don’t love how bright the dang thing is in my bedroom in the middle of the night. It could double as a very effective (but very unwanted) flashlight. The Chumby has a night-mode, but it is generally enabled by using a fixed time. That would be great if I went to bed and got up at the same time each day, but I don’t.

So, what I really wanted was a Chumby with an ambient light sensor. So…. I set out to make one:

Yes, using a propeller professional development board is a little overkill for the project, but it’s what I had on hand and will suffice until I can make something a little smaller.

The first part of the hack is a small program for the prop that samples a photocell every so often and sends either a “L” or a “D” out the serial port. The photocell is connected in series with a 0.1uf capacitor to pin 23 on the prop, and the PDB’s serial port’s tx is connected to pin 21. The PDB’s serial port is connected via a USB/Serial adapter to the USB port on the back of the Chumby. Here is the prop program:

OBJ
  serial: "FullDuplexSerial"
CON
  _clkmode = xtal1 + pll16x
  _xinfreq        = 5_000_000
  'settings on scott's PDB board
  photoPin = 23
  txPin = 21
  rxPin = 22
  ' settings on scott's USB proto board
  ' photoPin = 0
  ' txPin = 30
  ' rxPin = 31
VAR
  long parameter
  long time
  long wtavg
  long dark

pub main
  serial.start(rxPin, txPin, 1, 115200)

  init_sensor

  repeat
     read_sensor

     parameter := wtavg

     if wtavg > 200000
        dark :=1
        serial.tx("D")
     else
        dark := 0
        serial.tx("L")

     ' do some work
     waitcnt(clkfreq/1000*100 + cnt)

pub init_sensor
  wtavg := 0

  ctra[30..26] := %01000
  ctra[5..0] := photoPin
  frqa := 1

pub read_sensor
  time := (phsa - 624) #> 0
  wtavg := (wtavg * 90 / 100) + time/10 

  dira[photoPin] := outa[photoPin] := 1
  waitcnt(clkfreq/100_000 + cnt)
  phsa~
  dira[photoPin]~

The second part of the hack is a small bash script that runs on the chumby. It reads from a USB-Serial adapter at /dev/ttyUSB0. If it reads an “L”, then it sets the display to bright. If it reads a “D”, then it sets it to dim.

#! /bin/bash
stty -F /dev/ttyUSB0 cs8 115200 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -is
ig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts -hup
while [[ 1 ]]; do
  ch=`head -c 1 /dev/ttyUSB0`
  if [[ $ch == "L" ]]; then
      echo 100 > /sys/devices/platform/stmp3xxx-bl/backlight/stmp3xxx-bl/brightness
  fi
  if [[ $ch == "D" ]]; then
      echo 30 > /sys/devices/platform/stmp3xxx-bl/backlight/stmp3xxx-bl/brightness
  fi
done

I placed the script in /mnt/storage/dimmer.sh on the chumby and started it with:

nohup bash /mnt/storage/dimmer.sh < /dev/null > /dev/null &

There are ways to automate execution of the script on startup and I’ll probably do that. In the meantime, my chumby is battery-backed-up during outages, so it’ll never get rebooted anyway.

A video of the hack in action:

Using the PDB’s on-board serial-usb converter

A short follow up… In my tinkering I was also able to use the PDB’s on-board serial-usb converter by using pins 30 and 31 for tx/rx. These are the same pins used by the programming interface. It also works fine using a prop-plug for serial-USB conversion. So, one could probably implement this with as few components as:

propeller microcontroller
eeprom
prop-plug (ttl-serial to usb)
crystal
3.3v power supply
photocell
0.1uf capacitor

Everything needed to do it except the photocell is in the propeller education kit… I might try to prototype it on the PEK, just to show the minimalist approach. The photocell actually used to be in the PEK, but was removed due to ROHS restrictions. Nevertheless I got mine before then. I guess I like to live dangerously.

Update – Implemented on Parallax Propeller USB Proto-Board

The USB Proto-Board is a great deal, and only costs $25 – $30 depending on quantity. It already has almost all of the components we need, including the propeller microcontroller, usb to serial, voltage regulators, etc. The only components needed to be added are the photocell and 0.1uf capacitor. Here is a picture of the circuit prototyped out on the proto-board:

The remaining thing that needs to be done is to turn off the annoying blinky light on the USB-Serial. The proto-board uses an FTDI FT232R chip, and FTDI produces a tool called ft_prog that can be used to reconfigure the lights. I set the output for C1 to CBitBangI/O and that appeared to turn off the LED. You don’t need to mess with C0, as that LED is only used when data is received by the prop, which only happens (in our case) during programming.

Using the USB proto-board, we have a nice little implementation of the circuit.


Leave a Reply


four − = 2

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

New Kickstarter Project!

Interested in Nixie Tube projects? Check out my new Nixie Tube Calculator project on Kickstarter.

Copyright Notice

All content on this website is copyright Scott M. Baker and may not be used or reprinted without explicit permission.

Violators will be eaten by a grue.