Categories

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

 

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

VEML6070 ultraviolet light sensor and Arduino example

In this example we look at an VEML6070 UV sensor and connect it to an Arduino UNO

VEML6070 is an advanced ultraviolet (UV) light sensor with I2C protocol interface and designed by the CMOS process. It is easily operated via a simple I2C command. The active acknowledge (ACK) feature with threshold windows setting
allows the UV sensor to send out a UVI alert message. Under a strong solar UVI condition, the smart ACK signal can be easily implemented by the software programming. VEML6070 incorporates a photodiode, amplifiers, and analog / digital circuits into a single chip. VEML6070’s adoption of FiltronTM UV technology provides the best spectral sensitivity to cover UV spectrum sensing. It has an excellent temperature compensation and a robust refresh rate setting that does not use an external RC low pass filter.

VEML6070 has linear sensitivity to solar UV light and is easily adjusted by an external resistor. Software shutdown mode is provided, which reduces power consumption to be less than 1 μA. VEML6070’s operating voltage ranges from 2.7 V to 5.5 V.

You can find out about the UV index at the following link – https://en.wikipedia.org/wiki/Ultraviolet_index

This is the key chart from this site and one of the reasons that a UV index meter is so important

UV Index Media graphic color Risk of harm from unprotected sun exposure, for the average adult Recommended protection
0.0–2.9 Green “Low” A UV Index reading of 0 to 2 means low danger from the sun’s UV rays for the average person.Wear sunglasses on bright days. If you burn easily, cover up and use broad spectrum SPF 30+ sunscreen. Bright surfaces, such as sand, water and snow, will increase UV exposure.
3.0–5.9 Yellow “Moderate” A UV Index reading of 3 to 5 means moderate risk of harm from unprotected sun exposure.Stay in shade near midday when the sun is strongest. If outdoors, wear sun protective clothing, a wide-brimmed hat, and UV-blocking sunglasses. Generously apply broad spectrum SPF 30+ sunscreen every 2 hours, even on cloudy days, and after swimming or sweating. Bright surfaces, such as sand, water and snow, will increase UV exposure.
6.0–7.9 Orange “High” A UV Index reading of 6 to 7 means high risk of harm from unprotected sun exposure. Protection against skin and eye damage is needed.Reduce time in the sun between 10 a.m. and 4 p.m. If outdoors, seek shade and wear sun protective clothing, a wide-brimmed hat, and UV-blocking sunglasses. Generously apply broad spectrum SPF 30+ sunscreen every 2 hours, even on cloudy days, and after swimming or sweating. Bright surfaces, such as sand, water and snow, will increase UV exposure.
8.0–10.9 Red “Very high” A UV Index reading of 8 to 10 means very high risk of harm from unprotected sun exposure. Take extra precautions because unprotected skin and eyes will be damaged and can burn quickly.Minimize sun exposure between 10 a.m. and 4 p.m. If outdoors, seek shade and wear sun protective clothing, a wide-brimmed hat, and UV-blocking sunglasses. Generously apply broad spectrum SPF 30+ sunscreen every 2 hours, even on cloudy days, and after swimming or sweating. Bright surfaces, such as sand, water and snow, will increase UV exposure.
11.0+ Violet “Extreme” A UV Index reading of 11 or more means extreme risk of harm from unprotected sun exposure. Take all precautions because unprotected skin and eyes can burn in minutes.Try to avoid sun exposure between 10 a.m. and 4 p.m. If outdoors, seek shade and wear sun protective clothing, a wide-brimmed hat, and UV-blocking sunglasses. Generously apply broad spectrum SPF 30+ sunscreen every 2 hours, even on cloudy days, and after swimming or sweating. Bright surfaces, such as sand, water and snow, will increase UV exposure.

The easiest way to work with this sensor is to buy a module – this is a picture of the module that I bought

Connection

An I2C device so easy to get connected to an Arduino

arduino and veml6070

arduino and veml6070

Code

I used the Adafruit library – https://github.com/adafruit/Adafruit_VEML6070

#include <Wire.h>
#include "Adafruit_VEML6070.h"
 
Adafruit_VEML6070 uv = Adafruit_VEML6070();
 
void setup() 
{
  Serial.begin(9600);
  Serial.println("VEML6070 Test");
  uv.begin(VEML6070_1_T);  // pass in the integration time constant
}
 
 
void loop() 
{
  Serial.print("UV light level: "); 
  Serial.println(uv.readUV());
  delay(1000);
}

Output

Open the serial monitor – just as a note in my example I was indoors – hence the UV value was 0

VEML6070 Test
UV light level: 0
UV light level: 0
UV light level: 0
UV light level: 0

 

Link

You can pick up one of these sensors for about $2.50

UV sensor module VEML6070 UV Sensitivity Detection Sensor for Arduino

Share

MPL3115A2 pressure sensor and Arduino example

The MPL3115A2 is a compact, piezoresistive, absolute pressure sensor with an I2C digital interface. MPL3115A2 has a wide operating range of 20 kPa to 110 kPa, a range that covers all surface elevations on earth. The MEMS is temperature compensated utilizing an on-chip temperature sensor. The pressure and temperature data is fed into a high resolution ADC to provide fully compensated and digitized outputs for pressure in Pascals and temperature in °C.

The compensated pressure output can then be converted to altitude, utilizing the formula stated in Section 9.1.3 “Pressure/altitude” provided in meters.The internal processing in MPL3115A2 removes compensation and unit conversion load from the system MCU, simplifying system design

 

Schematics/Layout

 

arduino and mpl3115a2

arduino and mpl3115a2

 

Code

Again we use a library and again its an adafruit one – https://github.com/adafruit/Adafruit_MPL3115A2_Library

#include <Wire.h>
#include <Adafruit_MPL3115A2.h>
 
// Power by connecting Vin to 3-5V, GND to GND
// Uses I2C - connect SCL to the SCL pin, SDA to SDA pin
// See the Wire tutorial for pinouts for each Arduino
// http://arduino.cc/en/reference/wire
Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();
 
void setup() {
  Serial.begin(9600);
  Serial.println("Adafruit_MPL3115A2 test!");
}
 
void loop() {
  if (! baro.begin()) {
    Serial.println("Couldnt find sensor");
    return;
  }
 
  float pascals = baro.getPressure();
  // Our weather page presents pressure in Inches (Hg)
  // Use http://www.onlineconversion.com/pressure.htm for other units
  Serial.print(pascals/3377); Serial.println(" Inches (Hg)");
 
  float altm = baro.getAltitude();
  Serial.print(altm); Serial.println(" meters");
 
  float tempC = baro.getTemperature();
  Serial.print(tempC); Serial.println("*C");
 
  delay(250);
}

 

Output

Open the serial monitor – this is what I saw

Adafruit_MPL3115A2 test!
29.70 Inches (Hg)
84.12 meters
23.87*C
29.70 Inches (Hg)
84.25 meters
23.94*C

 

Links

https://www.nxp.com/docs/en/data-sheet/MPL3115A2.pdf

MPL3115A2 I2C Intelligent Temperature Pressure Altitude Sensor V2.0 For Arduino

Share

TCRT5000 Reflective Optical Sensor module example

In this example we look at a module based on the TCRT5000 Reflective Optical Sensor with Transistor Output. A very basic description of the TCRT500 is that it is an Infrared LED and an Infrared detector. It works by transmitting a beam of Infrared light downward towards a surface which is then reflected back and read by the detector

When the infrared transmitter emits a beam of light to a piece of paper, if the rays shine on a white surface, they will be reflected and in turn be received by the Infrared detector , and pin S on the module will output a low level; If the beam of light encounters a black line, they will be absorbed, thus the Infrared detector gets nothing, and pin S will output a high level.

So this module basically acts like a simple switch when it gets close to a white / black object. You can adjust the sensitivity with the potentiometer on the module

You have to be about 2.5cm from white paper but some objects you may have to be slightly closer for other objects. This is the module that I used – there are a couple of minor variations to this one but generally they all work the same way.

In this example, we will use an IR track sensor module and the on-board LED of an Arduino Uno to build a simple circuit to make a tracking line. You need to connect the pin S to digital pin 2 of the Uno board.

Connection

Code

Very easy to use

const int tcrtPin = 2; //the tracking module attach to pin 2
const int ledPin = 13;
 
void setup()
{
  pinMode(tcrtPin, INPUT); // set trackingPin as INPUT
  pinMode(ledPin, OUTPUT); //set ledPin as OUTPUT
}
 
void loop()
{
  boolean val = digitalRead(tcrtPin); // read the value of tcrt5000
  if(val == HIGH) //if it is HiGH
  { 
    digitalWrite(ledPin, LOW);
  }
  else
  {
    digitalWrite(ledPin, HIGH);
  }
}

 

Output

Cover and uncover the tcrt5000 sensor – the onboard LED of your Arduino should go on and off

 

Link

1 PCS Tracking Sensor IR TCRT5000 Infrared Obstacle Avoidance sensor for Arduino AVR PIC

Share