Thursday, 6 February 2014

Next Steps: Arduino Workshop 2

In this second workshop you will: 
  • select a transducer to measure something physical like temperature or illumination
  • download and install libraries to support components
  • connect neopixel smart RGB LEDs to your Arduino and change their colour and intensity under program control (some soldering required)
  • Use the neopixels to respond to the measured quantity, and more as time permits

or so I promised in the advertising. The simplest way to get some data would be following this lesson with a photocell. (Ignore all the parts about the LEDs for now and just make sure you can read an analog value that changes with the light. Take note of the extremes values for bright and dark. You can adapt the code at the bottom of this post.)

Adafruit NeoPixels are smart RGB LEDs that let you control a whole lot of LEDs with a single data line from the Arduino, and you don't have to worry about all those little current limiting resistors (although, if you are playing with more than a couple of neopixels, you should pay close attention to the capacitor and power supply comments in the uberguide). The complicated part is you need to control that one data line in a fairly complicated way. Fortunately somebody else has already done the work and there is a software library to make it easy. Download the zip file of the library and expand it, then install it in the libraries folder of the Arduino IDE. (You may have to create the libraries folder -- it goes in the same place as your Arduino sketchbook folder.) 

Be sure to remove the "-master" from the library folder name so it matches the name of the library. Then restart the Arduino IDE so it can find the library.

Put together a string of 2 or more NeoPixels (this will probably involve some soldering, either for the Flora or Breadboard versions), then hook them up to +5, Ground and pin 6 for a signal. Open the "strandtest" example in the NeoPixel library and set the number of NeoPixels you have in this line:
Adafruit_NeoPixel strip = Adafruit_NeoPixel(2, PIN, NEO_GRB + NEO_KHZ800);
When you run it you should see the pixels continuously changing colour and intensity independently of each other.

Modify the code to have the pixels respond to the analog input reading from from the photocell.

Other Sensors and Responses

You could measure temperature, or temperature and humidity, or colour, or orientation, with one of these sensors and use that measurement to control the colours and intensity of the NeoPixels. Download and install the library for your sensor, test that you can read it with the example programs, then combine the code from multiple examples to read the sensor, then control the NeoPixels based on what you read.

Take it further and get your sensor inputs to control a servo-motor, maybe opening and closing a greenhouse vent in response to changes in temperature or humidity.

Coding Hints

Pick one of the examples as your starting place and save it with a new name so you don't break the example.

Copy the header code from the top of all the files with the #include and variable declarations.

Copy any functions other than setup() and loop()

Copy and paste code from each of the setup() functions to make sure everything gets started properly.

Copy and paste code from the loop() functions to do the individual things you want.

This code will read analog values and report them:

//// ArduinoDAQ
// Kevin Hughes 2012
// Modified Rick Sellens July 2013

//// Constants
int d = 1;

void setup() {
  // All pins to input
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, INPUT);
  pinMode(A4, INPUT);
  pinMode(A5, INPUT);
  // Init Serial
  Serial.begin(57600);      // 57600 is highest I can sustain over X-Bee and FTDI
}// end setup

void loop() {
      //start line with time in ms, number of channels, full scale, then the values 
      Serial.print( millis() );          Serial.print(", 6, 1023, ");
      Serial.print( analogRead(A0) );    Serial.print(", ");
      Serial.print( analogRead(A0) );    Serial.print(", ");
      Serial.print( analogRead(A1) );    Serial.print(", ");
      Serial.print( analogRead(A2) );    Serial.print(", ");
      Serial.print( analogRead(A3) );    Serial.print(", ");
      Serial.print( analogRead(A4) );    Serial.print(", ");
      Serial.println( analogRead(A5) );
      while(millis()%100 != 0);            // repeat every 100 ms