Monthly Archives: August 2017

Tutorial: How to use the HC-SR505 PIR sensor with the Arduino

HC-SR505 PIR Sensor

HC-SR505 PIR Sensor.

PIR (Pyroelectric InfraRed) sensors measure infrared (IR) light that is radiating from objects in the detection area. The HC-SR505 is such a sensor. It very easy-to-use as it already delivers a HIGH signal if something is moving in the detection area. This HIGH signal lasts about 8 seconds, which is sometimes seen as a disadvantage. Unfortunately, this disadvantage can not be solved on software level, i.e. adding a workaround to the source code does not work (I’m almost 100% sure that this can only be solved by replacing one of the capacitors or resistors). The module has an operating voltage between 4.5V and 20V. According to the data sheet, the sensor detects movement up to a distance of 3m. In this tutorial, it is shown how to use the HC-SR505 sensor with the Arduino Platform.

List of materials:
– Arduino Uno [Search on Aliexpress | Amazon]
– Jumper wires [Search on Aliexpress | Amazon]
– Breadboard [Search on Aliexpress | Amazon]
– HC-SR505 [Search on Aliexpress | Amazon]

Pin layout

Pin layout used in this tutorial to wire the HC-SR505 PIR sensor to the Arduino Uno.

Pin layout used in this tutorial to wire the HC-SR505 PIR sensor to the Arduino Uno.

In order to wire the SR505 to the Arduino, you need only three wires. The SR505 has three pins which are marked on the back: “-“, “OUT”, and “+”. First, make sure that the SR505 is provided with enough voltage: One of the Arduino’s GND pins must be connected to the SR505’s “-” pin. Next, the Arduino’s 5V pin must be connected to the “+” pin of the SR505.
The remaining “OUT” pin returns either LOW or HIGH, depending on whether something was detected. This pin must be connected to one of the Arduino’s digital pins. In this tutorial, pin #2 is used.

Example source code
The program is very simple. First, a serial connection is opened in the setup function. The serial connection is used later to print the sensor’s state to the serial monitor. Then, pin #2 is declared as an input pin. In the loop function, the state of pin #2 is read. If the pin has a HIGH signal, the message “Movement detected.” is printed. Otherwise, if the pin has a LOW signal, a message is printed indicating that the sensor did not detect any movement. At the end of the loop function, a delay of a second is added to avoid flooding the serial monitor with too many messages.

// (c) Michael Schoeffler 2017, http://www.mschoeffler.de

#define SIGNAL_PIN 2

void setup()  
{
  Serial.begin(9600);
  pinMode(SIGNAL_PIN, INPUT);
}
void loop()  
{
  if(digitalRead(SIGNAL_PIN)==HIGH) {
    Serial.println("Movement detected.");
  } else {
    Serial.println("Did not detect movement.");
  }
  delay(1000);
}

When the code has been compiled and transferred to the Arduino, the serial monitor (“Tools”-> “Serial monitor”) should show the current state of the PIR sensor. You can easily trigger the sensor to detect something by moving your hand above the sensor.

Serial Monitor output sent by the HC-SR505 PIR sensor.

Serial Monitor output sent by the HC-SR505 PIR sensor.

 

Video Tutorial

How to control an LED pixel strip (WS2812B) with an Arduino and the FastLED library

WS2812B modules

On the left is an LED pixel strip which has WS2812B modules on it (30 WS2812B modules per meter). To the right are raw WS2812B modules (front and back).

LED pixel strips are strips which have small LED modules mounted on it. One of these LED modules is the WS2812B which features a cascading port transmission mechanism which needs only a single line.

This means that multiple WS2812B can be cascaded and individually controlled by a single connection for data transmission. Therefore, cascading many WS2812B modules requires only wiring for VCC, GND, and the data transmission. As a result, the WS2812B modules are well suited for LED pixel strips. Today, WS2812B-based LED pixel strips are available in all kinds of variants. Typically, strips can be bought with 30 to 144 WS2812B modules per meter. When having 144 modules per meter, there is almost no space left between the modules. In this tutorial it is shown how to program an Arduino in order to control a WS2812B-based LED strip with the FastLED library. FastLED is a library that allows to control all kinds of LED strips (WS2810, WS2811, LPD8806, Neopixel etc.).

List of materials:
– Arduino Nano [Search on Aliexpress | Amazon]
– Jumper wires [Search on Aliexpress | Amazon]
– Breadboard [Search on Aliexpress | Amazon]
– WS2812B-based LED strip [Search on Aliexpress | Amazon]

Pin layout:

This fritzing file shows a schematic of how to wire an Arduino Nano to the LED pixel strip.

This fritzing file shows a schematic of how to wire an Arduino Nano to the LED pixel strip.

Wiring up a basic setup is very easy, as the LED pixel strip has only three input pins which must be connected. Typically, these input pins represent GND, Vdd 5V, and a data connection. My LED pixel strip has three wires: white (GND), red (Vdd), and green (data). The color of the inputs might differ among manufacturers. Therefore, have a look at the description or datasheet of the LED pixel strip that you bought.
An Arduino can provide enough power to an LED strip with thirty WS2812B modules. For this reason, we can connect the GND pin and the 5V pin directly to the white and red wire, correspondingly. According to some forum entries, you get about 400mA from the Arduino’s 5V pin. Last but not least, we connect the data pin (green wire) to the Arduino’s pin 3. Basically, you can use any pin that supports PWM signals.

Example source code:
As mentioned before, the program utilizes the FastLED library to control the LED strip. At the beginning, the FastLED header file is included. In the setup function, the LED strip representation is initialized. As the FastLED supports many more LED strips (different LED modules, different lengths, etc.), the initialization needs the LED module type, Arduino pin number used for data transfer, color order, reference to an array representing the LEDs, and the number of LEDs. Optionally, a color correction can be set in order to improve the fidelity of the colors.
Next, there is a function which switches off all pixels (“showProgramCleanUp”). This function becomes very useful to “reset” the LED strip.
The example source code contains three different “light programs”. Each light program is implemented as a function. The first program switches on all LEDs in randomly selected colors (“showProgramRandom”). The next program shows a single-colored pixel being shifted from the beginning of the LED strip to the end (“showProgramShiftSinglePixel”). The last program shows how multiple pixel are shifted from the beginning to the end (“showProgramShiftMultiPixel”). In this light program, each pixel has a randomly selected color.
In the loop function, each light program is called twice with different parameters. Before a function call to a light program is made, all pixels of the LED strip are switched off by calling the special program “showProgramCleanUp”.

// (c) Michael Schoeffler 2017, http://www.mschoeffler.de
#include "FastLED.h"

#define DATA_PIN    3
#define LED_TYPE    WS2812B
#define COLOR_ORDER GRB
#define NUM_LEDS    30
#define BRIGHTNESS  96

CRGB leds[NUM_LEDS];

void setup() {
  delay(3000); // initial delay of a few seconds is recommended
  FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip); // initializes LED strip
  FastLED.setBrightness(BRIGHTNESS);// global brightness
}


// switches off all LEDs
void showProgramCleanUp(long delayTime) {
  for (int i = 0; i < NUM_LEDS; ++i) { 
    leds[i] = CRGB::Black;
  }
  FastLED.show();
  delay(delayTime);
}

// switches on all LEDs. Each LED is shown in random color. 
// numIterations: indicates how often LEDs are switched on in random colors
// delayTime: indicates for how long LEDs are switched on.
void showProgramRandom(int numIterations, long delayTime) {
  for (int iteration = 0; iteration < numIterations; ++iteration) { 
    for (int i = 0; i < NUM_LEDS; ++i) { 
      leds[i] = CHSV(random8(),255,255); // hue, saturation, value
    }
    FastLED.show();
    delay(delayTime);
  }
}

// Shifts a single pixel from the start of strip to the end. 
// crgb: color of shifted pixel
// delayTime: indicates how long the pixel is shown on each LED
void showProgramShiftSinglePixel(CRGB crgb, long delayTime) {
  for (int i = 0; i < NUM_LEDS; ++i) { 
    leds[i] = crgb;
    FastLED.show();
    delay(delayTime);
    leds[i] = CRGB::Black;
  }
}

// Shifts multiple pixel from the start of strip to the end. The color of each pixel is randomized.
// delayTime: indicates how long the pixels are shown on each LED
void showProgramShiftMultiPixel(long delayTime) {
  for (int i = 0; i < NUM_LEDS; ++i) { for (int j = i; j > 0; --j) { 
      leds[j] = leds[j-1];
    }
    CRGB newPixel = CHSV(random8(),255,255);
    leds[0] = newPixel;
    FastLED.show();
    delay(delayTime);
  }
}

// main program
void loop() {
  showProgramCleanUp(2500); // clean up
  showProgramRandom(100, 100); // show "random" program
  
  showProgramCleanUp(2500); // clean up
  showProgramRandom(10, 1000); // show "random" program

  
  showProgramCleanUp(2500); // clean up
  showProgramShiftSinglePixel(CRGB::Blue, 250); // show "shift single pixel program" with blue pixel
  
  showProgramCleanUp(2500); // clean up
  showProgramShiftSinglePixel(CRGB::Maroon, 100); // show "shift single pixel program" with maroon pixel

  
  showProgramCleanUp(2500); // clean up
  showProgramShiftMultiPixel(500); // show "shift multi pixel" program
  
  showProgramCleanUp(2500); // clean up
  showProgramShiftMultiPixel(50); // show "shift multi pixel" program
}

When the source code is compiled and transferred to the Arduino, the LED strip should show colored pixels according the active light program.

LED pixel strip controlled by an Arduino Uno.

LED pixel strip controlled by an Arduino Uno.

Video Tutorial