BadgerHack: Sensor Add-On Kit

Pages
Contributors: Sarah Al-Mutlaq
Favorited Favorite 2

Introduction

The BadgerStick that you received by visiting a SparkFun booth at one of the various events we’ve attended can be hacked to perform a wide variety of tasks. The BadgerStick is a fully capable microcontroller that can sense the environment around it.

BadgerHack

This tutorial will guide you through turning your BadgerStick into an environment sensing station. We’ll do so by adding a temperature sensor and a soil moisture sensor.

SparkFun BadgerStick and Sensor add-on kit with plant

Plant not included

NOTE: The BadgerStick and RedStick are two different products. The BadgerStick (aka BadgerHack) originated as an event-only platform to aid SparkFun in teaching soldering and programming at events like Maker local Faires and SXSW. The RedStick evolved from that concept and is the retail version of the BadgerStick, available for sale on SparkFun.com. All of the BadgerStick tutorials and expansion kits are compatible with both the BadgerStick and the RedStick, unless otherwise stated.

Required Materials

On top of your Badgerstick/Redstick and LED array display you will need a few more parts for this project:


Suggested Reading

Before starting this tutorial, we highly recommend you work through the main BadgerHack guide first.

BadgerHack

September 23, 2015

This tutorial shows users how to solder their SparkFun interactive badges as well as put them to use in other projects.

Additionally, if you are new to soldering or electronics, we recommend you check out the following:

When you are ready to start hacking your badge, we definitely recommend reading:

Hardware Hook-up

There is a little bit of soldering, so if you need a quick refresher on that I suggest taking a look at our soldering tutorial.

To begin, snap off 15 pins from the break-away headers, and solder them to the through-holes on the side opposite the LED array of the BadgerStick.

BadgerStick with headers

You can place the headers in the breadboard to help keep them in place as you solder.

Soldering BadgerStick with breadboard

The rest of the setup will be using the breadboard and the jumper wires.

Start by sticking the headers you just soldered to your stick at a corner of your breadboard.

Now we will connect the sensors as follows:

Component Pin Badger/Redstick Pin
Soil Moisture Sensor VCC VCC
Soil Moisture Sensor GND GND
Soil Moisture Sensor SIG A0
Temperature Sensor left GND
Temperature Sensor middle A4
Temperature Sensor right VCC

* Pins not listed are not used.


Here is a picture layout of how to connect everything up

Fritzing Diagram of badgerstick badgerhack add-on kit hookup

Fritzing diagram of a RedStick hookup (GND and VCC slightly different on a BadgerStick).

SparkFun BadgerStick and Sensor Add-on kit layout

IMPORTANT: You will need to power your Badgerstick/Redstick through its USB port or a USB extension cable, so that the analog sensors are getting a known voltage. You can cut the battery pack off, you can leave the battery pack attached but turned off, or you can power it through the battery pack. Just know that your temp reading will become less accurate the more your battery drains.

With everything hooked up, it’s time to upload some code.

Code

Analog sensors work by providing a voltage output that is proportional to the what they are measuring. This means with something like analog temp sensor we are using, the output voltage can be converted to temperature easily using the scale factor of 10 mV/°C. However, a little bit of math is required depending on what the input voltage is, so that will change based on if you are using the Badgerstick at 3.3V or the Redstick at 5V. (But don’t worry, this has already been done for you in the code.)

The soil moisture sensor works the same way, but we are leaving the data as is, since converting it to something usable like kg of water per kg or soil would be very difficult without knowing what kind of soil you have and how it reacts to electricity.

Plug the USB side of your BadgerStick into your computer. Make sure “BadgerStick” and the associated COM port are selected in the Arduino IDE, and click upload.

If you have the Redstick Make sure “Arduino Uno” and the associated COM port are selected before uploading. Also if you are using a Redstick, you will need to go into the code and change one variable at the end to have the correct temp output (instructions in the code).

language:c
/**
 * BadgerHack Sensor add on kit
 * Sarah Al-Mutlaq @ SparkFun Electronics
 * January 22, 2016
 * 
 * Scrolls temp reading and soil moisture across the Badger's LED array.
 * 
 * License: http://opensource.org/licenses/MIT
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 *NOTE: for this add on kit, you will want to power the badger
 *through its USB port, so that you know you have 3.3V instead 
 *of an unknown amount from a battery pack, which would make 
 *the temperature reading less accurate. 
 */

#include <SparkFun_LED_8x7.h>
#include <Chaplex.h>



// Global variables
static byte led_pins[] = {2, 3, 4, 5, 6, 7, 8, 9}; // Pins for LEDs
const int moisturePin = A0;     //signal pin from the moisture sensor
                                //is hooked up to pin A0 on the badger
const int temperaturePin = A4;  //singnal pin from temp sensor is 
                                //hooked up to pin A4 of the badger        

void setup() {

  // Initialize LED array
  Plex.init(led_pins);

  // Clear display
  Plex.clear();
  Plex.display();
}

void loop() {

  float voltage;

  // First we'll measure the voltage at the temperature analog pin. Normally
  // we'd use analogRead(), which returns a number from 0 to 1023.
  // Here we've written a function (further down) called
  // getVoltage() that returns the true voltage (0 to 5 Volts)
  // present on an analog input pin.

  voltage = getVoltage(temperaturePin);

  // Now we'll convert the voltage to degrees Celsius.
  // This formula comes from the temperature sensor datasheet:
  float tempC = (voltage - 0.5) * 100.0;
   // While we're at it, let's convert degrees Celsius to Fahrenheit.
  // This is the classic C to F conversion formula:
  float tempF = tempC * (9.0/5.0) + 32.0;
  char temperature[10];
  char str_tempC[6];
  char str_tempF[6];

  //This converts the floating variable of temperature to a 
  //sting so that we can print that on the array:

  /* 4 is mininum width, 2 is precision; float value is copied onto str_temp*/
  dtostrf(tempF, 4, 2, str_tempF);
  sprintf(temperature,"Temp: %s F", str_tempF);

  //dtostrf(tempC, 4, 2, str_tempC);               //Uncomment these lines and
  //sprintf(temperature,"Temp: %s C", str_tempC);  //comment the previous lines 
                                                   //If you want to see temp in C

  //This prints the string we just made to the badger array:
  Plex.scrollText(temperature, 1);

  //wait for 7 secounds while scrolling temp:
  delay(7000); 

  //Read the value of the moisture sensor and print that directly 
  //to the badger array:

  int sensorValue = analogRead(moisturePin);
  char moisture[20];

  sprintf(moisture, "Moisture level: %i", sensorValue);

  Plex.scrollText(moisture, 1);

  //wait for 11 seconds while scrolling moisture level:
  delay(11000); 

  // Stop scrolling the text
  Plex.stopScrolling();
  delay(2000);
}

float getVoltage(int pin)
{
  // This function has one input parameter, the analog pin number
  // to read. You might notice that this function does not have
  // "void" in front of it; this is because it returns a floating-
  // point value, which is the true voltage on that pin (0 to 5V).

  // You can write your own functions that take in parameters
  // and return values. Here's how:

    // To take in parameters, put their type and name in the
    // parenthesis after the function name (see above). You can
    // have multiple parameters, separated with commas.

    // To return a value, put the type BEFORE the function name
    // (see "float", above), and use a return() statement in your code
    // to actually return the value (see below).

    // If you don't need to get any parameters, you can just put
    // "()" after the function name.

    // If you don't need to return a value, just write "void" before
    // the function name.

  // Here's the return statement for this function. We're doing
  // all the math we need to do within this statement:


  return ((analogRead(pin) * 0.0048875855327468) * (3.3/5));

  // This equation converts the 0 to 1023 value that analogRead()
  // returns, into a 0.0 to 3.3 value that is the true voltage
  // being read at that pin.
  //If you are using a 5V device just erase the "*(3.3/5)" in
  //the above equation.
}

Resources and Going Further

With that, you should have an environment sensing station over which you have full control.

If you make something cool with your Badger, share it with #BadgerHack, or create a tutorial on hackster.io.

Like sensors? Here are a few more sensors that are pretty cool:

LIDAR-Lite v3

SEN-14032
149.99
11
SparkFun Simultaneous RFID Reader - M6E Nano

SEN-14066
199.95
1
FLiR Dev Kit

KIT-13233
259.95
24
SparkFun 9DoF Razor IMU M0

SEN-14001
49.95

Resources

Other BadgerHack Projects

Check out some of the other things you can make with the Badger:

BadgerHack: Gaming Add-On Kit

February 16, 2016

Make a Breakout clone with the BadgerHack Gaming Add-On Kit.