Things used in this project

Schematics

Schematics
Gy30

Code

GY30LightSensorC#
The GY-30 Driver
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Devices.I2c;

namespace GY30LightSensor
{
    public class GY30LightSensorEventArgs : EventArgs
    {
        public int? Lux { get; set; }

        public GY30LightSensorEventArgs(int? lux)
        {
            Lux = lux;
        }
    }

    public class GY30LightSensor
    {
        public int Bh1750Address => 0x23;

        public I2cDevice I2CLightSensor { get; private set; }

        private Timer PeriodicTimer { get; set; }

        public int TimerIntervalMs { get; set; }

        public event ReadingEventHandler Reading;

        public delegate void ReadingEventHandler(object sender, GY30LightSensorEventArgs e);

        private void OnReading(int lux)
        {
            Reading?.Invoke(lux, new GY30LightSensorEventArgs(lux));
        }

        public GY30LightSensor(int timerIntervalMs = 100)
        {
            TimerIntervalMs = timerIntervalMs;
        }

        public async Task InitLightSensorAsync()
        {
            string aqs = I2cDevice.GetDeviceSelector();
            /* Get a selector string that will return all I2C controllers on the system */
            var dis = await DeviceInformation.FindAllAsync(aqs);
            /* Find the I2C bus controller device with our selector string           */
            if (dis.Count == 0)
            {
                throw new FileNotFoundException("No I2C controllers were found on the system");
            }

            var settings = new I2cConnectionSettings(Bh1750Address)
            {
                BusSpeed = I2cBusSpeed.FastMode
            };

            I2CLightSensor = await I2cDevice.FromIdAsync(dis[0].Id, settings);
            /* Create an I2cDevice with our selected bus controller and I2C settings */
            if (I2CLightSensor == null)
            {
                throw new UnauthorizedAccessException(string.Format("Slave address {0} on I2C Controller {1} is currently in use by " +
                                 "another application. Please ensure that no other applications are using I2C.", settings.SlaveAddress, dis[0].Id));
            }

            /* Write the register settings */
            try
            {
                I2CLightSensor.Write(new byte[] { 0x10 }); // 1 [lux] aufloesung
            }
            /* If the write fails display the error and stop running */
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to communicate with device: " + ex.Message);
                throw;
            }

            PeriodicTimer = new Timer(this.TimerCallback, null, 0, TimerIntervalMs);
        }

        private void TimerCallback(object state)
        {
            var lux = ReadI2CLux();
            OnReading(lux);
        }

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

            // is this calculation correct?
            var valf = ((readBuf[0] << 8) | readBuf[1]) / 1.2; 

            return (int)valf;
        }
    }
}

Credits

20170310 041553193 ios (1) mka4nfm6gi
Edi Wang

ASP.NET and Windows 10 Developer

Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

Test Your Internet Speed Using a Raspberry Pi + Ubidots
Easy
  • 233
  • 4

Protip

Use your Raspberry Pi to measure any internet connection speed with Ubidots.

Chicken Coop Livestream
Easy
  • 2,161
  • 7

Full instructions

We have two chickens, and their coop is located at the back of the garden. I decided to build a Pi-based streaming device to check on them.

Raspberry Pi Shutdown / Restart Button
Easy
  • 5,087
  • 20

Full instructions

Building an installation project? Shut down or reboot your project safely, without a keyboard or SSH!

3d printed case for a RaspPi 3, an Arduino and a 5" display
Easy
  • 275
  • 4

Full instructions

A simple and compact frame to mount a 5" monitor, a Raspberry Pi, an Arduino, and a battery, which is extensible for other sensors.

DIY Raspberry Pi Indoor Outdoor Webcam
Easy
  • 1,346
  • 6

Full instructions

Ever want to monitor your home but don't want to buy a $100 1080p webcam? Well I did, and you can too!

DYI hydroponics system powered by UniPi
Easy
  • 1,339
  • 10

Full instructions

DYI cloud connected hydroponics system powered by UniPi and Cloud4RPi

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaFree StoreBlog