티스토리 뷰

반응형

LED has lots of types according to specification, how much spend the watt, how much bright and etc. I already have single color LED to develop based on RaspeberryPI and used it in my solution. In addition to that, I bought IR LED to turn on according to status of light, for example, IR Led is turned on when it is dark. Before making the circuit and would like to test this mechanism how it works. It needs to have rules like If brightness is too dark,  LED will be turned. Once I connect to the light sensor to measure how much I can get normally in my room. Here is what I used light sensor in this time.

Like the picture, GY-30 is the Sensor module which can detect the light. Here is detailed description.

  • Chip : BH1750FVI
  • Power Supply : 3 ~ 5V
  • Light Range : 0 ~ 65535lx
  • Sensor Built-in : 16 bit AD converter
  • Size(L x W) : Approx. 3.2cm x 1.5cm
  • Direct digital output, bypassing the complex calculation, bypassing the calibration
  • Don't distinguish between ambient light
  • Close to the spectral characteristics of visual acurity
  • Widely used to 1-lux high precision measurement
  • Standard NXP II2 communication agreement
Everywhere you can find it and it's description easier than other light sensor. Light sensor is to be trigger for the rule but I need to get action stuff like LED. Therefore I would buy RGB LED which is possible to blink 3 colors. Recently, MS IoT site has updated for new sample and decided to refer the RGB sample for that.

Already we know how to control LED with gpio pin and here is preparation what we need to test.

  • 754-1615-ND Tri-Color LED - http://www.digikey.com/product-detail/en/WP154A4SUREQBFZGC/754-1615-ND/3084119
  • 330 Register
  • 100 Register

The LED has 4 legs to connect for control. Read color is the first one from the left and needs to connection 330 register. Second one is ground. Third is Blue and the last one is green which need to connect 100 register. 

I do not believe it needs 3 gpio pin but the LED is able to control with each different pin and picked up 5, 6 and 13 number of gpio pin in RaspberryPI. Once I setup the LED and light sensor with RaspberryPI. RaspberryPI has one I2C and used it to measure light in real time. Let's see the my circuit.

Before starting the control, you can see the ambient light in the LED if you connected to RaspberryPI with Power. Here is the code for I2C and LED, how to work

 
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Diagnostics;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Threading;
using System.Threading.Tasks;
using Windows.Devices.Gpio;
using Windows.Devices.I2c;
using Windows.Devices.Enumeration;
using Windows.System.Threading;


namespace IR_CMERA_TEST
{

    public sealed partial class MainPage : Page
    {

        private const int LED_PIN_5 = 5;
        private const int LED_PIN_6 = 6;
        private const int LED_PIN_13 = 13;

        private const int ISR_PIN = 6;
        private GpioPin pin_5;
        private GpioPin pin_6;
        private GpioPin pin_13;
        private GpioPinValue pinValue;


        private I2cDevice GY30;
        private Timer periodicTimer;


        public MainPage()
        {
            this.InitializeComponent();
            InitGpio();
            InitDY30();
        }

        private void InitGpio()
        {
            var gpio = GpioController.GetDefault();
            if(gpio == null)
            {
                pin_5 = null;
                pin_6 = null;
                pin_13 = null;
                return;
            }
            // LED PIN Initialize
            pin_5 = gpio.OpenPin(LED_PIN_5);
            pin_6 = gpio.OpenPin(LED_PIN_6);
            pin_13 = gpio.OpenPin(LED_PIN_13);

            pinValue = GpioPinValue.High;
            pin_5.Write(pinValue);
            pin_6.Write(pinValue);
            pin_13.Write(pinValue);

            pin_5.SetDriveMode(GpioPinDriveMode.Output);
            pin_6.SetDriveMode(GpioPinDriveMode.Output);
            pin_13.SetDriveMode(GpioPinDriveMode.Output);

        }

        private async void InitDY30()
        {
            try
            {
                string aqs = I2cDevice.GetDeviceSelector();
                var dis = await DeviceInformation.FindAllAsync(aqs);
                var settings = new I2cConnectionSettings(0x23);
                settings.BusSpeed = I2cBusSpeed.FastMode;
                GY30 = await I2cDevice.FromIdAsync(dis[0].Id, settings);

                if (GY30 == null)
                {
                    Debug.WriteLine("Not able to ope GY30");
                    return;
                }
            }
            catch (Exception error)
            {
                Debug.WriteLine("Exception: " + error.Message);
            }
            //GY30.ConnectionSettings.
            periodicTimer = new Timer(TimerCallback, null, 0, 100);
        }

        private void TimerCallback(object state)
        {
            var lux = ReadI2CLux();
            Debug.WriteLine("LUX : " + lux);
            if(lux < 100)
            {
                pin_13.Write(GpioPinValue.High);
                pin_6.Write(GpioPinValue.Low);
                pin_5.Write(GpioPinValue.Low);
            }
            else if(lux >= 350)
            {
                pin_13.Write(GpioPinValue.Low);
                pin_6.Write(GpioPinValue.High);
                pin_5.Write(GpioPinValue.Low);
            }
            else if(lux > 100 && lux < 350)
            {
                pin_13.Write(GpioPinValue.Low);
                pin_6.Write(GpioPinValue.Low);
                pin_5.Write(GpioPinValue.High);
            }
            else
            {
                Debug.WriteLine("Error Case");
            }
        }

        private int ReadI2CLux()
        {
            byte[] regAddrBuf = new byte[] { 0x23 };
            byte[] readBuf = new byte[2];

            GY30.WriteRead(regAddrBuf, readBuf);

            var valf = ((readBuf[0] << 8) | readBuf[1]) / 1.2;
            return (int)valf;
        }
}

Light sensor will measure every 100 ms to give signal for the LED like if brightness is under 100, Red color will be turned on. For measuring the light, need to know the specification how to communicate via I2C. In order to control GY-30 sensor module, we have to set up the register into the sensor. 0x23 is basic field to get the light value and uses I2C api from Windows.Devices.I2c. The testing is easier than previous but I want to know how to work for RGB LED and will use RGB LED according to matched percent when someone's face detects. If a user is detected, Green color will be turned on otherwise RED color. Here is the result using the code and circuit. Let's take a look at it.

In my security Home solution, I would like to use RGB LED also, IR LED for different use cases.

반응형
댓글