Categories

Follow Us

Arduino and VL53L0X Time-of-Flight example

The VL53L0X is a new generation Time-of-Flight (ToF) laser-ranging module housed in the smallest package on the market today, providing accurate distance measurement whatever the target reflectances unlike conventional technologies. It can measure absolute distances up to 2m, setting a new benchmark in ranging performance levels, opening the door to various new applications.

The VL53L0X integrates a leading-edge SPAD array (Single Photon Avalanche Diodes) and embeds ST’s second generation FlightSenseTM patented technology.

Here is a typical module

The VL53L0X’s 940 nm VCSEL emitter (Vertical Cavity Surface-Emitting Laser), is totally invisible to the human eye, coupled with internal physical infrared filters, it enables longer ranging distances, higher immunity to ambient light, and better robustness to cover glass optical crosstalk.

 

Schematics/Layout

 

arduino and VL53L0X

arduino and VL53L0X

 

Code

 

You will need to install the adafruit library – https://github.com/adafruit/Adafruit_VL53L0X

#include "Adafruit_VL53L0X.h"
 
Adafruit_VL53L0X lox = Adafruit_VL53L0X();
 
void setup() {
  Serial.begin(115200);
 
  // wait until serial port opens for native USB devices
  while (! Serial) {
    delay(1);
  }
 
  Serial.println("Adafruit VL53L0X test");
  if (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    while(1);
  }
  // power 
  Serial.println(F("VL53L0X API Simple Ranging example\n\n")); 
}
 
 
void loop() {
  VL53L0X_RangingMeasurementData_t measure;
 
  Serial.print("Reading a measurement... ");
  lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!
 
  if (measure.RangeStatus != 4) {  // phase failures have incorrect data
    Serial.print("Distance (mm): "); Serial.println(measure.RangeMilliMeter);
  } else {
    Serial.println(" out of range ");
  }
 
  delay(100);
}

 

Output

Open the serial monitor, move the sensor nearer and farther away from objects for example your hand

VL53L0X API Simple Ranging example

Reading a measurement… Distance (mm): 70
Reading a measurement… Distance (mm): 70
Reading a measurement… Distance (mm): 71
Reading a measurement… Distance (mm): 72
Reading a measurement… Distance (mm): 71
Reading a measurement… Distance (mm): 71
Reading a measurement… Distance (mm): 70
Reading a measurement… Distance (mm): 71
Reading a measurement… Distance (mm): 67
Reading a measurement… Distance (mm): 114

 

Links

https://www.st.com/resource/en/datasheet/vl53l0x.pdf

1pcs GY- 530 VL53L0X World smallest Time-o f-Flight (ToF) laser ranging sensor 10 orders

Share

SHT20 temperature sensor and Arduino example

In this example we will connect a SHT20 temperature sensor to an Arduino Uno. Lets look at some information about the sensor

The SHT20 humidity and temperature sensor of Sensirion has become an industry standard in terms of form factor and intelligence: Embedded in a reflow solderable Dual Flat No leads (DFN) package of 3 x 3mm foot print and 1.1mm height it provides calibrated, linearized sensor signals in digital, I2C format.

The SHT2x sensors contain a capacitive type humidity sensor, a band gap temperature sensor and specialized analog and digital integrated circuit – all on a single CMOSens® chip. This yields in an unmatched sensor performance in terms of accuracy and stability as well as minimal power consumption.

Every sensor is individually calibrated and tested. Lot identification is printed on the sensor and an electronic identification code is stored on the chip – which can be read out by command. Furthermore, the resolution of SHT2 can be changed by command (8/12bit up to 12/14bit for RH/T) and a checksum helps to improve communication reliability.

 

Parts List

 

Amount Part Type
1 Temperature and humidity detection sensor module SHT20
1 UNO R3 CH340G/ATmega328P, compatible for Arduino UNO R3

 

Schematics

Couldn’t find a good part for fritzing

Its an I2C device that needs 3.3v and GND, so its not that difficult to wire

 

Code

There is a library to make things easy and it is – https://github.com/DFRobot/DFRobot_SHT20

#include <Wire.h>
#include "DFRobot_SHT20.h"
 
DFRobot_SHT20    sht20;
 
void setup()
{
    Serial.begin(9600);
    Serial.println("SHT20 Example!");
    sht20.initSHT20();                                  // Init SHT20 Sensor
    delay(100);
    sht20.checkSHT20();                                 // Check SHT20 Sensor
}
 
void loop()
{
    float humd = sht20.readHumidity();                  // Read Humidity
    float temp = sht20.readTemperature();               // Read Temperature
    Serial.print("Time:");
    Serial.print(millis());
    Serial.print(" Temperature:");
    Serial.print(temp, 1);
    Serial.print("C");
    Serial.print(" Humidity:");
    Serial.print(humd, 1);
    Serial.print("%");
    Serial.println();
    delay(1000);
}

 

Output

Open the serial monitor and you should see something similar to this

SHT20 Example!
End of battery: no
Heater enabled: no
Disable OTP reload: yes
Time:203 Temperature:25.3C Humidity:48.6%
Time:1306 Temperature:25.2C Humidity:48.7%
Time:2410 Temperature:25.1C Humidity:48.9%
Time:3514 Temperature:25.1C Humidity:49.0%

Here is a video showing the code being compiled and the serial monitor being opened, I put my finger on the sensor to raise the temperature

 

Links

Sensirion_Humidity_Sensors_SHT20_Datasheet.pdf

 

Share

Neomatrix scrolling text example on an Arduino

In a previous example we used Neopixel libraries and had a couple of code examples. Now we look at a couple of different libraries, the example built in scrolls text on the screen

AliExpress.com Product – WS2812 LED 5050 RGB 8×8 64 LED Matrix for Arduino

Connection

neomatrix and arduino

neomatrix and arduino

Code Example

You need the Adafruit GFX library, Adafruit Neopixel and Adafruit Neomatrix libraries – all of these are installable via the library manager

// Adafruit_NeoMatrix example for single NeoPixel Shield.
 
#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>
#ifndef PSTR
 #define PSTR // Make Arduino Due happy
#endif
 
#define PIN 3
 
// MATRIX DECLARATION:
// Parameter 1 = width of NeoPixel matrix
// Parameter 2 = height of matrix
// Parameter 3 = pin number (most are valid)
// Parameter 4 = matrix layout flags, add together as needed:
//   NEO_MATRIX_TOP, NEO_MATRIX_BOTTOM, NEO_MATRIX_LEFT, NEO_MATRIX_RIGHT:
//     Position of the FIRST LED in the matrix; pick two, e.g.
//     NEO_MATRIX_TOP + NEO_MATRIX_LEFT for the top-left corner.
//   NEO_MATRIX_ROWS, NEO_MATRIX_COLUMNS: LEDs are arranged in horizontal
//     rows or in vertical columns, respectively; pick one or the other.
//   NEO_MATRIX_PROGRESSIVE, NEO_MATRIX_ZIGZAG: all rows/columns proceed
//     in the same order, or alternate lines reverse direction; pick one.
//   See example below for these values in action.
// Parameter 5 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
 
 
// Example for NeoPixel Shield.  In this application we'd like to use it
// as a 5x8 tall matrix, with the USB port positioned at the top of the
// Arduino.  When held that way, the first pixel is at the top right, and
// lines are arranged in columns, progressive order.  The shield uses
// 800 KHz (v2) pixels that expect GRB color data.
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(8, 8, PIN,
  NEO_MATRIX_TOP     + NEO_MATRIX_RIGHT +
  NEO_MATRIX_COLUMNS + NEO_MATRIX_PROGRESSIVE,
  NEO_GRB            + NEO_KHZ800);
 
const uint16_t colors[] = {
  matrix.Color(255, 0, 0), matrix.Color(0, 255, 0), matrix.Color(0, 0, 255) };
 
void setup() 
{
  matrix.begin();
  matrix.setTextWrap(false);
  matrix.setBrightness(5);
  matrix.setTextColor(colors[0]);
}
 
int x    = matrix.width();
int pass = 0;
 
void loop() 
{
  matrix.fillScreen(0);
  matrix.setCursor(x, 0);
  matrix.print(F("Arduino"));
  if(--x < -36) {
    x = matrix.width();
    if(++pass >= 3) pass = 0;
    matrix.setTextColor(colors[pass]);
  }
  matrix.show();
  delay(100);
}

All going well Arduino will scroll on the Neomatrix display

Links

WS2812 LED 5050 RGB 8×8 64 LED Matrix for Arduino

Share

WS2812 8×8 64 LED Matrix Arduino examples

I like the WS2812 Neopixel boards, modules and LEDs you can buy and have been looking at some of the variants but coming at over $35 for some of these I baulked at buying them, recently I found compatible boards for a fraction of the price and decided to but them. The first is an WS2812 8×8 64 LED Matrix LED 5050 module

Warning :  Each LED is capable of drawing as much as 60mA (at peak brightness the matrix can draw just over 3.5 Amps at 5 Volts), this adds up so it is suggested to use a 5V 2A power supply. For most uses, you’ll see about 1-2A of current per panel. I tend to use the lower values in the code examples you will see later

Solder wires or a header to the input port and supply power to the +5V and ground pins, then connect the DIN pin to a pin of your microcontroller

 

Schematics/Layout

 

 

 

 

 

Code Examples

Example 1

#include <Adafruit_NeoPixel.h>
 
// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN            3
 
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      64
 
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
 
int delayval = 10; // delay for half a second
 
void setup() 
{
  pixels.begin(); // This initializes the NeoPixel library.
}
 
void loop() {
 
  // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
 
  for(int i=0;i<NUMPIXELS;i++)
  {
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(1,0,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    //delay(delayval);
  }
  delay(1000);
  for(int i=0;i<NUMPIXELS;i++)
  {
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(0,1,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    //delay(delayval);
  }
  delay(1000);
  for(int i=0;i<NUMPIXELS;i++)
  {
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(0,0,1)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    //delay(delayval);
  }
  delay(1000);
}

Example 2

#include <Adafruit_NeoPixel.h>
 
// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN            3
 
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      64
 
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
 
int delayval = 10; // delay for half a second
 
void setup() 
{
  pixels.begin(); // This initializes the NeoPixel library.
}
 
void loop() {
 
  // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
 
  for(int i=0;i<16;i++)
  {
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(1,0,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    //delay(delayval);
  }
  //delay(1000);
  for(int i=16;i<32;i++)
  {
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(0,1,0)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    //delay(delayval);
  }
  //delay(1000);
  for(int i=32;i<64;i++)
  {
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(0,0,1)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    //delay(delayval);
  }
  //delay(1000);
}

Example 3

include <Adafruit_NeoPixel.h>
 
// Which pin on the Arduino is connected to the NeoPixels?
#define PIN            3
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      64
 
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
 
int delayval = 10; // delay for half a second
 
 
void setup() 
{
  pixels.begin(); // This initializes the NeoPixel library.
  randomSeed(analogRead(0));
}
 
void loop() {
 
  // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
  int rndRedValue = random(0,5);
  int rndGreenValue = random(0,5);
  int rndBlueValue = random(0,5);
  for(int i=0;i<NUMPIXELS;i++)
  {
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(rndRedValue,rndGreenValue,rndBlueValue)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    //delay(delayval);
  }
  delay(1000);
}

Example 4

#include <Adafruit_NeoPixel.h>
 
// Which pin on the Arduino is connected to the NeoPixels?
#define PIN            3
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS      64
 
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
 
int delayval = 10; // delay for half a second
 
 
void setup() 
{
  pixels.begin(); // This initializes the NeoPixel library.
  randomSeed(analogRead(0));
}
 
void loop() {
 
  // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one.
  int rndRedValue = random(0,5);
  int rndGreenValue = random(0,5);
  int rndBlueValue = random(0,5);
  int rndLED = random(0,64);
  for(int i=0;i<rndLED;i++)
  {
    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(rndRedValue,rndGreenValue,rndBlueValue)); // Moderately bright green color.
    pixels.show(); // This sends the updated pixel color to the hardware.
    //delay(delayval);
  }
  delay(1000);
}

 

Links

WS2812 LED 5050 RGB 8×8 64 LED Matrix for Arduino

Share