Using a Photocell + MCP3008 + Raspberry Pi + Pi_piper gem ## Enabling SPI on the Raspberry Pi To enable SPI on the Raspberry Pi, at the command-line: 1. type `raspi-config` 2. select *9 Advanced Options*, then select *A6 SPI* 3. select *Yes* to enable, then *Yes* to load the SPI kernel module by default ## Connections -[http://www.jamesrobertson.me.uk/dynarex/2016/feb/19/photocell-mcp3008-raspberry-pi-connections.xml] ## Example require 'pi_piper' #port of the Adafruit MCP3008 interface code found @ http://learn.adafruit.com/send-raspberry-pi-data-to-cosm/python-script def read_adc(adc_pin, clockpin, adc_in, adc_out, cspin) cspin.on clockpin.off cspin.off command_out = adc_pin command_out |= 0x18 command_out <<= 3 (0..4).each do adc_in.update_value((command_out & 0x80) > 0) command_out <<= 1 clockpin.on clockpin.off end result = 0 (0..11).each do clockpin.on clockpin.off result <<= 1 adc_out.read if adc_out.on? result |= 0x1 end end cspin.on result >> 1 end clock = PiPiper::Pin.new :pin => 18, :direction => :out adc_out = PiPiper::Pin.new :pin => 23 adc_in = PiPiper::Pin.new :pin => 24, :direction => :out cs = PiPiper::Pin.new :pin => 25, :direction => :out adc_pin = 0 value = read_adc(adc_pin, clock, adc_in, adc_out, cs) # raw reading from value when the room light was on #=> 614 # raw reading from value when the room light was off #=> 918 The above example results in a raw value which can be used to help indicate the ambient lighting level. ## Resources * Raspberry GPIO pins http://www.jamesrobertson.me.uk/dynarex/2015/sep/02/raspberry-gpio-pins.xml * Photocell + MCP3008 + Raspberry Pi connections http://www.jamesrobertson.me.uk/dynarex/2016/feb/19/photocell-mcp3008-raspberry-pi-connections.xml * Microchip MCP3008 I P 10Bit ADC 16DIP 8CH SPI http://www.jamesrobertson.me.uk/cpages/2016/feb/16/microchip-mcp3008-i-p-10bit-adc-16dip-8ch-spi-ebay-co-uk.html * pi_piper/mcp3008.rb at develop ยท jwhitehorn/pi_piper https://github.com/jwhitehorn/pi_piper/blob/develop/examples/mcp3008/mcp3008.rb * Analogue Sensors On The Raspberry Pi Using An MCP3008 http://www.raspberrypi-spy.co.uk/2013/10/analogue-sensors-on-the-raspberry-pi-using-an-mcp3008/ * Measuring the ambient light intensity using a photocell http://www.jamesrobertson.me.uk/arduino/2016/feb/16/measuring-the-ambient-light-intens.html mcp3008 pi_piper analog photocell raspberrypi