Introduction: Display Sensor Data on WS2811

About: Studying Communication & Multimedia design @ Amsterdam University of Applied Sciences

This tutorial will show you how to visualize data from an HC-SR04 distance sensor on the WS2811 ledstrip.

It consists of an Nodemcu Esp8266, An WS2811 Led-Strip, a HC-SR04 Distance sensor and some wires.

Enjoy!

Step 1: Supplies

To make this project you will need the following:

1) 1x Node MCU
2) HC - SR04 Ultrasonic Distance Measuring Sensor*
3) A PC or a Mac to connect the Arduino board
4) Arduino Software
5) Wires to connect the hardware

Step 2: Hardware Set-up

First we need to make the WS2812B Ledstrip connectable. Grab the soldering machine and wait till it is warm enough to use. Strip the wires and solder them like the image above. I used black for Ground (GND), Red for +5v and white for the digital pin. Connect the wires to the NodeMCU like this:

Pins & Wires WS2812B:
Ground - Gnd
+5V - 3V3
Digital pin - Any (I Used D5)

Finished? Allright, now it is time to connect the HC-SR04 Distance sensor. This sensor already has pins so it will take a little less time. Connect the wires to the NodeMCU like this:

Pins & Wires HC-SR04:
Ground - Gnd
Vcc - Vin
Echopin - D0
Trigpin - D1

Step 3: Setting Up the Arduino

WS2812B

To program the Arduino we need to connect the NodeMCU to any pc or mac that has the Arduino software installed. To play with the WS2812B Led-Strip we need to download the Fastled Library. This Library will help creating animations and supports a wide range of popular leds.

Installing the Library:

1) Go to https://github.com/FastLED/FastLED/releases and download the library (.zip file)
2) Open Arduino Software > Sketch > Include Library > Add .zip library
3) Navigate to the .zip file's location and open it. (As shown in the image above)
4) Navigate to Sketch > Import Library menu. You should now see the library.

Now we need to run some basic code to see whether the Led-Strip is working or not. This is also the start of our code.

/ defines pins numbers

#include <FastLED.h>

#define LED_PIN     5 // Connected Wire
#define NUM_LEDS    10 // Number of Leds on strip
#define BRIGHTNESS  64
#define LED_TYPE    WS2811
#define COLOR_ORDER GRB
CRGB leds[NUM_LEDS];

#define UPDATES_PER_SECOND 100

void setup() {
  delay( 3000 ); // power-up safety delay
    FastLED.addLeds(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
    FastLED.setBrightness(  BRIGHTNESS );
  
Serial.begin(115200); // Starts the serial communication
}

void loop() {
      // Turn the first led red for 1 second
      leds[0] = CRGB::Red; 
      FastLED.show();
      delay(1000);
      
      // Set the first led back to black for 1 second
      leds[0] = CRGB::Black;
      FastLED.show();
      delay(1000);
    }

This should make the first Led blink in red.

HC-SR04

To use the HC-SR04 Sensor we need to add some basic code. The following code will help us.

const int trigPin = D1; // trigPin connected to D1
const int echoPin = D0; // echoPin connected to D0<br>// defines variables
long duration;
int distance;

void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(115200); // Starts the serial communication
}<br>void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance)<br>}

Now we have an variable called distance which is as it says the distance the HC-SR04 measures.

Step 4: Combine the Code

Now it is time to combine the sensor data from the HC-SR04 Sensor and show something on the WS2812b Led strip. If you combine the code from the previous step you get the following code.

// defines pins numbers
#include <FastLED.h>

#define LED_PIN 5 // Connected Wire #define NUM_LEDS 10 // Number of Leds #define BRIGHTNESS 64 #define LED_TYPE WS2811 //Type #define COLOR_ORDER GRB CRGB leds[NUM_LEDS];

#define UPDATES_PER_SECOND 100<br> const int trigPin = D1; const int echoPin = D0; // defines variables long duration; int distance;

void setup() { delay( 3000 ); // power-up safety delay FastLED.addLeds(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); FastLED.setBrightness( BRIGHTNESS ); pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input Serial.begin(115200); // Starts the serial communication }

void loop() { // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance distance= duration*0.034/2; // Prints the distance on the Serial Monitor Serial.print("Distance: "); Serial.println(distance);

// Add code here

}

This is the perfect template to mess around with.

Do something when distance is something
Now we want to make something happen at a certain distance. By adding the following code it is possible to run code when the distance is less than 20 centimeters from the sensor.

if (distance  < 20) { 				// distance (from sensor) is < (less than) 20
// Something happens }

It is also possible to do this with larger than, or even make combinations.

Step 5: Create Something Usefull

Now it is time to use your knowledge to design and create something useful. I tried to show the amount of bottles in a fridge by displaying them on the Led-Strip. (See illustration).

In this example the WS2812B is attached to the refrigerator and the HC-SR04 is placed in the inside of the fridge.
The Led-strip changes every time a bottle is taken from the fridge. It turns orange when more than 50% is taken. And all the leds turn red when the fridge is empty.

To realize this i made the following code:

// defines pins numbers
#include <FastLED.h>

#define LED_PIN 5 #define NUM_LEDS 33 #define BRIGHTNESS 64 #define LED_TYPE WS2811 #define COLOR_ORDER GRB CRGB leds[NUM_LEDS];

#define UPDATES_PER_SECOND 100

const int trigPin = D1; const int echoPin = D0; // defines variables long duration; int distance;

void setup() { delay( 3000 ); // power-up safety delay FastLED.addLeds(leds, NUM_LEDS).setCorrection( TypicalLEDStrip ); FastLED.setBrightness( BRIGHTNESS ); pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output pinMode(echoPin, INPUT); // Sets the echoPin as an Input Serial.begin(115200); // Starts the serial communication }

void loop() { // Clears the trigPin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Sets the trigPin on HIGH state for 10 micro seconds digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds duration = pulseIn(echoPin, HIGH); // Calculating the distance distance= duration*0.034/2; // Prints the distance on the Serial Monitor Serial.print("Distance: "); Serial.println(distance);

//in de buurt if (distance < 5) { leds[0] = CRGB::Green; FastLED.show(); leds[1] = CRGB::Green; FastLED.show(); leds[2] = CRGB::Green; FastLED.show(); leds[3] = CRGB::Green; FastLED.show(); leds[4] = CRGB::Green; FastLED.show(); leds[5] = CRGB::Green; FastLED.show(); leds[6] = CRGB::Green; FastLED.show(); leds[7] = CRGB::Green; FastLED.show(); leds[8] = CRGB::Green; FastLED.show(); leds[9] = CRGB::Green; FastLED.show(); }

if (distance > 5 && distance < 10) { leds[0] = CRGB::Green; FastLED.show(); leds[1] = CRGB::Green; FastLED.show(); leds[2] = CRGB::Green; FastLED.show(); leds[3] = CRGB::Green; FastLED.show(); leds[4] = CRGB::Green; FastLED.show(); leds[5] = CRGB::Green; FastLED.show(); leds[6] = CRGB::Green; FastLED.show(); leds[7] = CRGB::Green; FastLED.show(); leds[8] = CRGB::Green; FastLED.show(); leds[9] = CRGB::Black; FastLED.show(); }

if (distance > 10 && distance < 15) { leds[0] = CRGB::Green; FastLED.show(); leds[1] = CRGB::Green; FastLED.show(); leds[2] = CRGB::Green; FastLED.show(); leds[3] = CRGB::Green; FastLED.show(); leds[4] = CRGB::Green; FastLED.show(); leds[5] = CRGB::Green; FastLED.show(); leds[6] = CRGB::Green; FastLED.show(); leds[7] = CRGB::Green; FastLED.show(); leds[8] = CRGB::Black; FastLED.show(); leds[9] = CRGB::Black; FastLED.show(); }

if (distance > 15 && distance < 20) { leds[0] = CRGB::Green; FastLED.show(); leds[1] = CRGB::Green; FastLED.show(); leds[2] = CRGB::Green; FastLED.show(); leds[3] = CRGB::Green; FastLED.show(); leds[4] = CRGB::Green; FastLED.show(); leds[5] = CRGB::Green; FastLED.show(); leds[6] = CRGB::Green; FastLED.show(); leds[7] = CRGB::Black; FastLED.show(); leds[8] = CRGB::Black; FastLED.show(); leds[9] = CRGB::Black; FastLED.show(); }

if (distance > 20 && distance < 25) { leds[0] = CRGB::Orange; FastLED.show(); leds[1] = CRGB::Orange; FastLED.show(); leds[2] = CRGB::Orange; FastLED.show(); leds[3] = CRGB::Orange; FastLED.show(); leds[4] = CRGB::Orange; FastLED.show(); leds[5] = CRGB::Orange; FastLED.show(); leds[6] = CRGB::Black; FastLED.show(); leds[7] = CRGB::Black; FastLED.show(); leds[8] = CRGB::Black; FastLED.show(); leds[9] = CRGB::Black; FastLED.show(); }

if (distance > 25 && distance < 30) { leds[0] = CRGB::Orange; FastLED.show(); leds[1] = CRGB::Orange; FastLED.show(); leds[2] = CRGB::Orange; FastLED.show(); leds[3] = CRGB::Orange; FastLED.show(); leds[4] = CRGB::Orange; FastLED.show(); leds[5] = CRGB::Black; FastLED.show(); leds[6] = CRGB::Black; FastLED.show(); leds[7] = CRGB::Black; FastLED.show(); leds[8] = CRGB::Black; FastLED.show(); leds[9] = CRGB::Black; FastLED.show(); }

if (distance > 30 && distance < 35) { leds[0] = CRGB::Orange; FastLED.show(); leds[1] = CRGB::Orange; FastLED.show(); leds[2] = CRGB::Orange; FastLED.show(); leds[3] = CRGB::Orange; FastLED.show(); leds[4] = CRGB::Black; FastLED.show(); leds[5] = CRGB::Black; FastLED.show(); leds[6] = CRGB::Black; FastLED.show(); leds[7] = CRGB::Black; FastLED.show(); leds[8] = CRGB::Black; FastLED.show(); leds[9] = CRGB::Black; FastLED.show(); }

if (distance > 35 && distance < 40) { leds[0] = CRGB::Orange; FastLED.show(); leds[1] = CRGB::Orange; FastLED.show(); leds[2] = CRGB::Orange; FastLED.show(); leds[3] = CRGB::Black; FastLED.show(); leds[4] = CRGB::Black; FastLED.show(); leds[5] = CRGB::Black; FastLED.show(); leds[6] = CRGB::Black; FastLED.show(); leds[7] = CRGB::Black; FastLED.show(); leds[8] = CRGB::Black; FastLED.show(); leds[9] = CRGB::Black; FastLED.show(); } if (distance > 40 && distance < 45) { leds[0] = CRGB::Red; FastLED.show(); leds[1] = CRGB::Red; FastLED.show(); leds[2] = CRGB::Black; FastLED.show(); leds[3] = CRGB::Black; FastLED.show(); leds[4] = CRGB::Black; FastLED.show(); leds[5] = CRGB::Black; FastLED.show(); leds[6] = CRGB::Black; FastLED.show(); leds[7] = CRGB::Black; FastLED.show(); leds[8] = CRGB::Black; FastLED.show(); leds[9] = CRGB::Black; FastLED.show(); }

if (distance > 40 && distance < 44) { leds[0] = CRGB::Red; FastLED.show(); leds[1] = CRGB::Black; FastLED.show(); leds[2] = CRGB::Black; FastLED.show(); leds[3] = CRGB::Black; FastLED.show(); leds[4] = CRGB::Black; FastLED.show(); leds[5] = CRGB::Black; FastLED.show(); leds[6] = CRGB::Black; FastLED.show(); leds[7] = CRGB::Black; FastLED.show(); leds[8] = CRGB::Black; FastLED.show(); leds[9] = CRGB::Black; FastLED.show(); leds[10] = CRGB::Black; FastLED.show(); } if (distance > 45) { leds[0] = CRGB::Red; FastLED.show(); leds[1] = CRGB::Red; FastLED.show(); leds[2] = CRGB::Red; FastLED.show(); leds[3] = CRGB::Red; FastLED.show(); leds[4] = CRGB::Red; FastLED.show(); leds[5] = CRGB::Red; FastLED.show(); leds[6] = CRGB::Red; FastLED.show(); leds[7] = CRGB::Red; FastLED.show(); leds[8] = CRGB::Red; FastLED.show(); leds[9] = CRGB::Red; FastLED.show(); } }

That's all, Thank you for participating and I'd love to see any of your projects!