In this project we will use the Adafruit Starter Pack for Windows 10 IoT Core on Raspberry Pi 2 components to make a speaking light sensor. This will show how to use an MCP3008 Analog Digital Converter (ADC) chip to interface the Pi2 to three analog components. Two variable resistors (potentiometers) and one CdS photocell.
Hardware setup
Connect the Raspberry Pi2 to the breadboard and the other components as per the Fritzing diagram below.
Note: While setting up the circuit, make sure your MCP3008 chip is oriented correctly. The chip has a half-moon shape marker along with a dot on one side. This should be oriented as shown in the diagram below.
Optional
If you have a pair of headphones with a 1/8″ jack or a set of powered speakers with a 1/8″ jack you can connect them to the Pi2 audio output jack to hear the prompts from the speech system.
Code
MainPage.cs
You can download the code starting project from https://github.com/ms-iot/adafruitsample and we will lead you through the addition of the code needed to talk to the web service and get your pin on the map. What map?
Open up “Lesson_204\StartSolution\Lesson_204.sln“ and open the mainpage.xaml.cs file.
We have filled in a few methods as a starting point for you in this solution. If you want to jump ahead you can find a solution with all the code completed at:”Lesson_204\FullSolution\Lesson_204.sln”
Add the following lines at the top of the MainPage class.
// Use for configuration of the MCP3008 class voltage formula
const float ReferenceVoltage = 5.0F;
// Values for which channels we will be using from the ADC chip
const byte LowPotentiometerADCChannel = 0;
const byte HighPotentiometerADCChannel = 1;
const byte CDSADCChannel = 2;
// Some strings to let us know the current state.
const string JustRightLightString = "Ah, just right";
const string LowLightString = "I need a light";
const string HighLightString = "I need to wear shades";
// Some internal state information
enum eState { unknown, JustRight, TooBright, TooDark};
eState CurrentState = eState.unknown;
// Our ADC Chip class
MCP3008 mcp3008 = new MCP3008(ReferenceVoltage);
// The Windows Speech API interface
private SpeechSynthesizer synthesizer;
// A timer to control how often we check the ADC values.
public Timer timer;
Now add these lines to the MainPage constructor to setup the windows speech synthesizer and the ADC chip.
// Create a new SpeechSynthesizer instance for later use.
synthesizer = new SpeechSynthesizer();
// Initialize the ADC chip for use
mcp3008.Initialize();
Now add these lines to the OnNavigatedTo method. This will setup a timer callback which will call our code once per second on a different thread.
For more detail: Bright or Not?