Smart Light v1.0

smartlampv1.jpg
Smart Light v1.0

Over the past few months I have been working on a project, dubbed the “Smart Light”, that allows a user to control a connected appliance based on the ambient light in a room. I decided to gift this to the person who requested it so I put the final touches on it just after Christmas. With the hardest part over, installation and tuning were a breeze and I’m excited to take what I’ve learned from this project and build on it.

I made only a few minor changes but, overall, not much has changed since my last update. I had to replace the AC/DC converter I was using to power the Trinket as it failed on me. I’ve discovered that quite a few old dumb-phone chargers output 5V and varying mAh so I’ve begun a collection of name brand chargers for future projects like this. As you can see from the picture, I mounted the analogue light sensor inside the smallest Hammond case I could find at a local electrical supply shop. I braided the 3 wires to the sensor so that it would be easy to work with when installing the system. Also, I recently discovered heat shrink with hotmelt glue and used it extensively in this project as there were a number of exposed connections that normal heat shrink couldn’t handle.

With the project installed and working, I’m quite happy how it turned out but I do intend to make changes in both the hardware and software for the next version. The sketch for this project is simple and boils down to: if the light value is below a specific value, activate the relay else do nothing. The problem with this logic is that the light values fluctuate so that the value read by the sensor may jump above and below the threshold value. This jumping leads to the relay putting on a light show as it is rapidly activated and deactivated. To fix this, I am going to have the system activate the relay only after a certain number of readings in a row are below the threshold. This should drastically reduce the flickering effect when the appropriate value is found.

On the hardware side, my main concerns are minimizing the size of the system, adding the ability to adjust the threshold value that determines when to activate the relay and adding flexibility in placing the sensor. The biggest use of space in the case is the wiring so for my next version I’d like to move as much of the wiring and components to PCB. Right now, the only way to adjust the threshold value for the system is to modify the whole sketch and then re-upload it to the system. This is hardly ideal so I’d like to add a knob or series of buttons that allow the user to change the value. I’d even go so far as to add a small LCD screen to allow the user to set custom values and to see what the value. Lastly, I’d like to investigate the cost of making the sensor wireless so that the user isn’t limited by the length of the sensor wires when installing the system. Make the sensor wireless also opens the door to expanding the system to include other types of sensor allowing for further automation and monitoring of the home. I could see motion sensors being added to the system to trigger a security camera.

Adafruit 10DOF Sensor Library for BeagleBone Black

Screen Shot 2016-01-02 at 3.11.25 PM
L3GD20 Gyro Ouput in CSV Format

As part of my ongoing rover project, I have started writing C++ libraries for the various sensors and actuators. The first device I have begun to tackle writing a library for is the Adafruit 10DOF sensor. This sensor has libraries available for Arduino boards and in Python. Writing library code for the sensor hasn’t been without issues but has been a great learning experience. There are a lots of online tutorials but I have found that Derek Molloy’s tutorials have been consistently helpful. I recently bought Derek Molloy’s book Exploring BeagleBone: Tools and Techniques for Building with Embedded Linux to aid me in working with the BeagleBone Black. I did so because I found his freely available tutorials extremely thorough and have had the most success following them compared to other material online. His book includes the material in his tutorials plus more including  introductory topics such as administering Linux and basic electronic components to advanced topics such as interacting with the onboard Programmable Real-time Units (PRUs).

To start writing the driver for the 10DOF, I focused on one sensor at a time. I took the Arduino code for each sensor provided by Adafruit and went about replacing all calls to the Wire library to appropriate calls to the libmraa library. The 10DOF sensor uses the I2C bus to communicate with all the sensors and, initially, I used Derek Molloy’s I2CDevice class to handle communication. After some success with this class, I decided to use Intel’s libmraa for handling all communication between the BeagleBone  Black and peripherals. I made this switch because Molloy’s class is not as complete as libmraa and only deals with I2C communication whereas the libmraa api includes SPI, GPIO, UART and PWM functionality. This means I can use libmraa for controlling the motors and other sensors instead of having to write my own code or relying on separate libraries for sensors using different communication interfaces.

Writing the driver for each sensor was not as simple as replacing some functions in the Arduino code. The datatypes in the Arduino code had to be changed to match those recognized by the g++ compiler while staying true to the datatype used to store the data in the sensor’s registers. This required a close reading of each sensor’s data sheet to make sure I was storing the register data in variables with the same datatype. I won’t go into fine detail as to what changes I made since you can see the code yourself and compare it to the original Adafruit Arduino code.

There were a few times while working with the 10DOF sensor when I lost connection to it. This can occur when a register on a sensor is mishandled causing the sensor lock up becoming unresponsive. To deal with these lock ups when working remotely, I wired a P2N2222 transistor into the 10DOF sensor circuit so that I could power cycle the sensor if it became unresponsive. This has happened a couple of times while developing the driver software. I’ve been careful to make sure I’m properly working with the registers but mistakes still happen. Thankfully no damage was done and I haven’t needed to use this feature yet.

I’m still working on the driver and will post another update when it’s completed. The code for the BMP180, L3GD20 and LSM303 sensors can be found on my github page. Just make sure you have the libmraa library installed on your BeagleBone Black. This code is still under development and is provided with no warranty so use it at your own risk. If you have any comments on the code, please use the github issues page. My plan is to roll these separate libraries into one library for use with the 10DOF sensor but I will keep the repos for each sensor for those who buy the sensors individually.