NightLight Hat Board For Raspberry Pi SKU: EP-0098

From 52Pi Wiki
Jump to navigation Jump to search

NightLight Hat Board For Raspberry Pi

Description

NightLight Hat Board For Raspberry Pi is a Hat Board which have 8 WS2812 RGB LED lights.
And this Hat Board will communicate with Raspberry Pi via I2C protocol.
You can control each LED's color individually by command.
At the same time, you can also adjust the brightness of each LED by command.
The light board is connected to the GPIO pin by HAT.
You can stack multiple identical boards in a layered manner, which can make your lighting more layered.
It is recommended not to exceed four layers at most. Otherwise, the power supply may be insufficient.
Because all the lights need to be powered from the GPIO pin of the Raspberry Pi.
The feature of the HAT board is that you don't have to worry about messy cables.
If you only use one layer of light board, you can engrave your favorite pattern on an acrylic board with a laser engraving machine,
and then insert it vertically into the random acrylic bracket,
so that by controlling the light, you can make different The acrylic art board presents a different brilliance, turning your light board into a work of art.

Features

  • Easy to setup
  • 8xRGB WS2812 Lights
  • Extend the GPIO Pins
  • Can stack 4 pieces
  • Can Stack with other Stack board such as Relay module
  • Individual control of each Light
  • With beautiful acrylic light guide,Two factory-made patterns, two of them are blank for you DIY
  • Plug and Play
  • One command control

Gallery

Package Includes

  • 1 x NightLight Hat Board For Raspberry Pi
  • 4 x Screws & Nuts
  • 5 x Acrylic Pattern Design

How to use it

  • Turn on the I2C interface

Open a terminal and Run sudo raspi-config
Use the down arrow to select 5 Interfacing Options.
Arrow down to P5 I2C .
Select yes when it asks you to enable I2C.
Also select yes if it asks about automatically loading the kernel module.
Use the right arrow to select the <Finish> button.
Select yes when it asks to reboot.

  • Detect the register of the chip on board

i2cdetect -y 1
you will see this result:

Nightlighti2c09.png


  • Lights up one light

i2cset -y 1 0x15 0x01 0xff

 This register "0x15" means the MCU address of this board.
"0x01" means the first light's first color of the board, every light
has a register address, and you can follow this chart, "0xff" means give the light 100% power
and "0x00" will kill the light, you can put the value from "0x00" to "0xff" to make a dim light.

Register Map

Register Number Color Light number
0x01 red color No.1
0x02 green color No.1
0x03 blue color No.1
0x04 red color No.2
0x05 green color No.2
0x06 blue color No.2
0x07 red color No.3
0x08 green color No.3
0x09 blue color No.3
0x0a red color No.4
0x0b green color No.4
0x0c blue color No.4
0x0d red color No.5
0x0e green color No.5
0x0f blue color No.5
0x10 red color No.6
0x11 green color No.6
0x12 blue color No.6
0x13 red color No.7
0x14 green color No.7
0x15 blue color No.7
0x16 red color No.8
0x17 green color No.8
0x18 blue color No.8
0x19 button button register

Example Demo

  • Shell script

samle script to light up all LED's individually in a loop.

#!/bin/bash 
# lights up each LED individually in a loop.
while true
do 
  for i in `seq 1 9`
  do 
    for x in a b c d e f 
    do 
      i2cset -y 1 0x15 0x0$i 0x00    # turn off all LEDs.
    done
  done
   # lights up first line
   i2cset -y 1 0x15  0x01  0x55   # turn on No.1 led in half power.
   i2cset -y 1 0x15  0x04  0x55   # turn on No.2 led in half power.
   sleep 0.1                      # delay for a while.
   i2cset -y 1 0x15  0x01  0xff   # turn on No.1 led in full power.
   i2cset -y 1 0x15  0x04  0xff   # turn on No.2 led in full power.
   sleep 0.5 
   i2cset -y 1 0x15 0x01  0x00    # turn off  No.1 led 
   i2cset -y 1 0x15 0x04  0x00    # turn off  No.2 led 
    
   # lights up second line 
   i2cset -y 1 0x15 0x07 0x55
   i2cset -y 1 0x15 0x0a 0x55
   sleep 0.1
   i2cset -y 1 0x15 0x07 0xff
   i2cset -y 1 0x15 0x0a 0xff            
   sleep 0.5
   i2cset -y 1 0x15 0x07 0x00
   i2cset -y 1 0x15 0x0a 0x00

   i2cset -y 1 0x15 0x08 0x55
   i2cset -y 1 0x15 0x0b 0x55
   sleep 0.1
   i2cset -y 1 0x15 0x08 0xff
   i2cset -y 1 0x15 0x0b 0xff
   sleep 0.5
   i2cset -y 1 0x15 0x08 0x00
   i2cset -y 1 0x15 0x0b 0x00

   i2cset -y 1 0x15 0x09 0x55
   i2cset -y 1 0x15 0x0c 0x55
   sleep 0.1
   i2cset -y 1 0x15 0x09 0xff
   i2cset -y 1 0x15 0x0c 0xff
   sleep 0.5
   i2cset -y 1 0x15 0x09 0x00
   i2cset -y 1 0x15 0x0c 0x00

   i2cset -y 1 0x15 0x0d 0x55
   i2cset -y 1 0x15 0x10 0x55
   sleep 0.1
   i2cset -y 1 0x15 0x0d 0xff
   i2cset -y 1 0x15 0x10 0xff
   sleep 0.5
   i2cset -y 1 0x15 0x0d 0x00
   i2cset -y 1 0x15 0x10 0x00

   i2cset -y 1 0x15 0x0e 0x55
   i2cset -y 1 0x15 0x11 0x55
   sleep 0.1
   i2cset -y 1 0x15 0x0e 0xff
   i2cset -y 1 0x15 0x11 0xff
   sleep 0.5
   i2cset -y 1 0x15 0x0e 0x00
   i2cset -y 1 0x15 0x11 0x00

   i2cset -y 1 0x15 0x0f 0x55
   i2cset -y 1 0x15 0x12 0x55
   sleep 0.1
   i2cset -y 1 0x15 0x0f 0xff
   i2cset -y 1 0x15 0x12 0xff
   sleep 0.5
   i2cset -y 1 0x15 0x0f 0x00
   i2cset -y 1 0x15 0x12 0x00

   i2cset -y 1 0x15 0x13 0x55
   i2cset -y 1 0x15 0x16 0x55
   sleep 0.1
   i2cset -y 1 0x15 0x13 0xff
   i2cset -y 1 0x15 0x16 0xff
   sleep 0.5
   i2cset -y 1 0x15 0x13 0x00
   i2cset -y 1 0x15 0x16 0x00

   i2cset -y 1 0x15 0x14 0x55
   i2cset -y 1 0x15 0x17 0x55
   sleep 0.1
   i2cset -y 1 0x15 0x14 0xff
   i2cset -y 1 0x15 0x17 0xff
   sleep 0.5
   i2cset -y 1 0x15 0x14 0x00
   i2cset -y 1 0x15 0x17 0x00

   i2cset -y 1 0x15 0x15 0x55
   i2cset -y 1 0x15 0x18 0x55
   sleep 0.1
   i2cset -y 1 0x15 0x15 0xff
   i2cset -y 1 0x15 0x18 0xff
   sleep 0.5
   i2cset -y 1 0x15 0x15 0x00
   i2cset -y 1 0x15 0x18 0x00
   sleep 0.5
done

save this script in a file named "nightlight.sh" and then give it executive permission: chmod +x nightlight.sh
sudo ./nightlight.sh


  • Test DEMO in Language C

Please make sure your Raspberry Pi has been already connected to internet
In this demo, please make sure you have installed wiringPi liberary before you compile this C code.

 
sudo apt-get update 
sudo apt-get -y install git-core 
cd 
git clone git://git.drogon.net/wiringPi 
cd ~/wiringPi 
git pull origin 
cd ~/wiringPi 
./build
    • Test wiringPi’s installation

run the gpio command to check the installation:

$ gpio -v
$ gpio readall

That should give you some confidence that it’s working OK.
And then create a file called "nightlight.c" and write following code:

//  import the liberay.
#include <iostream>
#include <unistd.h>
#include <errno.h>
#include <wiringPiI2C.h>

using namespace std;    // define the namespace.
int press_times = 0;    
int interval = 1;
int button_addr = 0x19;   //button's register is 0x19.
int light_number = 0x01;  // the light is No.1 and color is red.
int key_times = 0;

int main()
{
        int fd, result;
        fd = wiringPiI2CSetup(0x15);   // initialized wiringPi i2c environment.
        cout << "Init result: " << fd << endl;   // print a message.
        result = wiringPiI2CReadReg16(fd, button_addr);     // read button's value from the register address.
        //printf("data is %d\n", result);
        // detect the button and if the button is pressed, it will turn on 2 RGB light in one row.
        // once you pressed twice, it will change the color to green, 3times to blue,  and then next 2 lights in one row.
        for(;;){
        result = wiringPiI2CReadReg16(fd, button_addr);    
        if (result > 0)
        {
                if(key_times % 2 == 0 )
                {
                wiringPiI2CWriteReg16(fd, light_number, 0xff);
                wiringPiI2CWriteReg16(fd, light_number+3, 0xff);
                }
                else
                {
                wiringPiI2CWriteReg16(fd, light_number, 0x00);
                wiringPiI2CWriteReg16(fd, light_number+3, 0x00);
                switch(light_number)
                {
                        case 0x01:
                        case 0x02:
                        case 0x07:
                        case 0x08:
                        case 0x0d:
                        case 0x0e:
                        case 0x13:
                        case 0x14: light_number++;break;
                        case 0x03:
                        case 0x09:
                        case 0x0F: light_number+=4; break;
                        case 0x15: light_number=0x01; break;
                }
                }
                key_times++;
                if(key_times > 1) key_times = 0;
                wiringPiI2CWriteReg16(fd, button_addr, 0x0);
                result = wiringPiI2CReadReg16(fd, button_addr);
                printf("data is %d\n", result);
        }
        else
        {
         printf("press the button\n");
         sleep(interval);
        }
     }
}

save it and then compile it by this command:
g++ nightlight.c -lwiringPi -o nightlight
run it:
sudo ./nightlight


  • Python script

you can also using Python script to driver the board.
firt, make sure you have installed "RPi.GPIO" liberary by import RPi.GPIO in python idle.
open a terminal and input python and then:
>>>import RPi.GPIO
if return nothing means the RPi.GPIO liberary is installed.
and then Create a new file named " nightlight.py" and copy and paste this code:

#!/usr/bin/env python
#
import time as t
import os


light_matrix = ['0x01','0x02','0x03','0x04','0x05','0x06','0x07','0x08','0x09','0x0a','0x0b','0x0c','0x0d','0x0e','0x0f','0x10','0x11','0x12','0x13','0x14','0x15','0x16','0x17','0x18']
flag = False
interval = 0.1

def turn_on_light():
    for i in range(len(light_matrix)):
        cmd = 'i2cset -y 1 0x15 ' + light_matrix[i] + ' 0xff'
        os.system(cmd)
        t.sleep(0.5)

def turn_off_light():
    for x in range(len(light_matrix)):
        cmd2 = 'i2cset -y 1 0x15 ' + light_matrix[x] + ' 0x00'
        os.system(cmd2)
        t.sleep(0.5)



if __name__=='__main__':
    try:
        flag = True

        while flag:
            turn_on_light()
            t.sleep(0.1)
            turn_off_light()
            t.sleep(0.1)
    except KeyboardInterrupt as e:
        print("Quit the Loop")

save it and run: python nightlight.py