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

Setting Up Your Pi Cap on the Raspberry Pi 1, 2 or 3
Easy
  • 64
  • 2

Protip

Follow this tutorial to set up your Pi Cap with a Raspberry Pi 1 A+/B+, Raspberry Pi 2 or Raspberry Pi 3.

Setting Up Your Pi Cap on the Raspberry Pi Zero
Easy
  • 72
  • 2

Protip

Follow this tutorial to set up your Pi Cap with a Raspberry Pi Zero.

One Small Step For a Yedi, One Giant Leap for Mankind
Easy
  • 148
  • 4

Work in progress

Counting your steps with the Calliope Mini. Hearing the Star Wars theme is always a huge motivator.

Secure Simple Remote Access for Camera Viewing
Easy
  • 2,001
  • 8

Full instructions

Turn your RPi into a full featured camera with remote access, end2end encryption, and easy user enrollment.

Microsoft Workshop Setup Guide
Easy
  • 705
  • 8

Protip

A collection of screencasts for setting up your laptop for the Hackster/Microsoft IoT Roadshow events.

PiCorder
Easy
  • 3,626
  • 14

Full instructions

A simple Pi-based camcorder using the very nice HyperPixel touchscreen from Pimoroni.

ProjectsCommunitiesTopicsContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login