Humidity-sensing LED Flower

Contributors: Dia, Member #313449
Favorited Favorite 5


This tutorial will show you how to make your own LED flower that senses humidity, just like the one in this week’s episode of ElectriCute! In case you missed it, here’s the video explaining exactly how this circuit works:

This is a pretty versatile project. It lives in my terrarium, and could reasonably go pretty much anywhere, including a garment or accessory. The only reason I didn’t go that route is our location: Colorado is a very boring place to sense humidity!

Humidity-sensing flower in terrarium

Continue reading to learn how to make your own.

Suggested Reading

Before getting started with this tutorial, there are a few concepts with which you should be familiar. Consider reading some of the tutorials below, before continuing on with this one.

How to Solder: Through-Hole Soldering

This tutorial covers everything you need to know about through-hole soldering.

Working with Wire

How to strip, crimp, and work with wire.

What is an Arduino?

What is this 'Arduino' thing anyway?


An introduction to I2C, one of the main embedded communications protocols in use today.


Here’s what you’re going to need for this circuit. The RGB LED flower is exclusive to the 21st Century Fashion Kit, but you can substitute a standard RGB LED if you’d rather!

The Circuit

Here’s your circuit, in all its glory:

circuit diagram

Click image for a closer look.

The Fritzing diagram makes it look nearly impossible to tell the pins of the RGB LED apart, but it really isn’t! The longest leg is ground. The single pin to one side of ground is red, and the other two pins, moving out from ground, are green, then blue.

If you’re using the flower, rather than a standard RGB LED, be very careful if you use heatshrink over the connections – it’s very easy to melt the flower petals with a hot-air gun!

The Code

Not sure how to upload code to your Arduino? No problem! Take a look our tutorial explaining everything in detail. Remember to select the correct board and port for your application.

Copy and paste the code below into the IDE, and click Upload:

ElectriCute: Humidity Sensing LED Flower
Dia Campbell
SparkFun Electronics
Oct. 29, 2014
Based off of the code found on,
which in turn was based of the wiring code at
With a few additions from Arduino example code 
Basically, if this code was a dog, you'd have gotten it from the pound.

Development environment specifics:
Arduino IDE 1.0+
Arduino Pro 5V 16MHz

This code is beerware; if you see us at the local pub, 
and you've found our code helpful, please buy us a round!

 int SHT_clockPin = 3;  // pin used for clock
 int SHT_dataPin  = 2;  // pin used for data
 int redPin = 4; // Define RGB pins
 int bluePin = 6;
 int greenPin = 5;

void setup(){
  Serial.begin(9600); // open serial at 9600 bps
pinMode(redPin, OUTPUT); //set RGB pins as outputs
pinMode(bluePin, OUTPUT);
pinMode(greenPin, OUTPUT);

 void loop(){
  //these can take a bit to get the values (100ms or so)
  float temperature = getTemperature();
  float humidity = getHumidity();

   Serial.print(" | ");

    if (humidity > 25) {  //humidity threshold; change to suit your purpose
    digitalWrite(bluePin, HIGH);  //When humidity is high enough
   digitalWrite(greenPin, HIGH); //all three pins are high
  digitalWrite(redPin, HIGH);
  else {
    digitalWrite(bluePin, LOW);  //when humidity is too low
    digitalWrite(greenPin, LOW); //turn off green and blue
    digitalWrite(redPin,HIGH);  //turn on red
   delay(1000);               // wait for a second
  digitalWrite(redPin, LOW);    // turn the red LED off 
  delay(1000);               // wait for a second

float getTemperature(){
  //Return Temperature in Celsius
  SHT_sendCommand(B00000011, SHT_dataPin, SHT_clockPin);

  int val = SHT_getData(SHT_dataPin, SHT_clockPin);
  SHT_skipCrc(SHT_dataPin, SHT_clockPin);
  return (float)val * 0.01 - 40; //convert to celsius

float getHumidity(){
  //Return  Relative Humidity
  SHT_sendCommand(B00000101, SHT_dataPin, SHT_clockPin);
  int val = SHT_getData(SHT_dataPin, SHT_clockPin);
  SHT_skipCrc(SHT_dataPin, SHT_clockPin);
  return -4.0 + 0.0405 * val + -0.0000028 * val * val; 

void SHT_sendCommand(int command, int dataPin, int clockPin){
  // send a command to the SHTx sensor
  // transmission start
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  digitalWrite(dataPin, HIGH);
  digitalWrite(clockPin, HIGH);
  digitalWrite(dataPin, LOW);
  digitalWrite(clockPin, LOW);
  digitalWrite(clockPin, HIGH);
  digitalWrite(dataPin, HIGH);
  digitalWrite(clockPin, LOW);

  // shift out the command (the 3 MSB are address and must be 000, the last 5 bits are the command)
  shiftOut(dataPin, clockPin, MSBFIRST, command);

   // verify we get the right ACK
   digitalWrite(clockPin, HIGH);
   pinMode(dataPin, INPUT);

  if (digitalRead(dataPin)) Serial.println("ACK error 0");
  digitalWrite(clockPin, LOW);
  if (!digitalRead(dataPin)) Serial.println("ACK error 1");

void SHT_waitForResult(int dataPin){
  // wait for the SHTx answer
  pinMode(dataPin, INPUT);

  int ack; //acknowledgement

  //need to wait up to 2 seconds for the value
  for (int i = 0; i < 1000; ++i){
   ack = digitalRead(dataPin);
   if (ack == LOW) break;

  if (ack == HIGH) Serial.println("ACK error 2");

 int SHT_getData(int dataPin, int clockPin){
  // get data from the SHTx sensor

  // get the MSB (most significant bits)
  pinMode(dataPin, INPUT);
  pinMode(clockPin, OUTPUT);
  byte MSB = shiftIn(dataPin, clockPin, MSBFIRST);

  // send the required ACK
  pinMode(dataPin, OUTPUT);
  digitalWrite(dataPin, HIGH);
  digitalWrite(dataPin, LOW);
  digitalWrite(clockPin, HIGH);
  digitalWrite(clockPin, LOW);

  // get the LSB (less significant bits)
  pinMode(dataPin, INPUT);
  byte LSB = shiftIn(dataPin, clockPin, MSBFIRST);
  return ((MSB << 8) | LSB); //combine bits

void SHT_skipCrc(int dataPin, int clockPin){
  // skip CRC data from the SHTx sensor
  pinMode(dataPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  digitalWrite(dataPin, HIGH);
 digitalWrite(clockPin, HIGH);
 digitalWrite(clockPin, LOW);

Once the code is uploaded, open the Serial Monitor. This should open a window which is scrolling numbers. The right column is temperature, the left column is humidity. Take a look at the number your humidity is hovering around, and use that to determine what you’d like your ideal humidity to be.

The line addressing humidity threshold is commented, and pretty close to the top, so look for that and make adjustments as needed. I set mine to just a hair above ambient humidity, because I wanted it to be easy to demo. You may want yours to be a bit less sensitive. In that same section, you can make changes to the way the lights react to those changes!

When the sensor is reading below that threshold, the LED blinks red, like so:

alt text

And, when it’s above the threshold, a nice, white glow is emitted:

alt text

Once you’ve got your reactivity and behavior where you’d like them, you’re all set! Unhook the board from the computer, plug the battery into the Arduino, and put your new humidity sensing device in your favorite terrarium, buttonhole, fascinator, or whatever you choose!

Resources and Going Further

Thanks for reading this project tutorial. Check out these other great ElectriCute videos for more project inspiration:

Want more 21st Century Fashion Kit fun? Check out these other projects: