A simple night light

In northern Norway it is dark almost all day in the winter, and I had an idea to make a simple night light that would turn automatically when ever it was needed. Perfect for a smooth transition between the pitch dark bedroom and the flood lighted bathroom. A simple motion detector connected to a low power LED should be able to light the hallway sufficiently to avoid any danger related to stumbling around in the dark.

The parts

The night light should be as simple as possible. I considered building my own motion detector, using a micro controller and IR LED and receiver. This, as I found out in so many projects, was overkill and simple solutions already exist. I bought a PIR sensor (Passive Infrared Sensor) from eBay, paying $2 for two of them.

PIR sensor

I also bought 10 1W LEDs for about $2.

LEDs

I am not sure how much power the PIR can deliver, so I might need a transistor to run current through the LED. The BC337 is rated at 800mA continuously, which should be more than enough for the 1W LED.

BC337 Transistor

The casing is a small plastic box I had lying around.

The encasing

And I needed a battery pack to power the whole thing

Batterypack

Planning the setup

During engineering school I was taught how to use OrCad. This corporate, pay-till-your-fingers-bleed, awful program that had all kinds of backward solutions. A program where Ctrl-z was the shortcut to mess up something important with no shortcut to get it back. Where you had to spend 2 hours drawing a new component because it spat out error messages that might as well have been in Arabic because it gave no sense.

Luckily, the open source community has made a awesome, free, easy to use, alternative. They even have the support and love of CERNKiCad was used extensively  through the bachelor thesis, and I came to love it.

A quick schematic drawing in KiCad gives the project an overview, and simplifies any troubleshooting. If you are a bit rusty it also helps getting the right component in the right place. Those NPN/PNP always gets mixed up!

PIR sensor technical drawing

Testing the setup

Instead of gluing and soldering the whole thing and hoping it works I prefer to test the setup in a breadboard first. Sometimes, especially when you are very sure you know how stuff works, it does not do what you thought it would. A breadboard test is a good way to sort out any brain farts you had while thinking of the solution.

The breadboard testing is a crucial part of any project like this. A missed connection, a short, LED connected backwards... Any small mistake is easy to fix while nothing is soldered or glued in place.
The breadboard testing is a crucial part of any project like this. A missed connection, a short, LED connected backwards… Any small mistake is easy to fix while nothing is soldered or glued in place.

After connecting 4,5V to GND and ground to VCC, realizing that I made that connection mistake and fixing it, it was time to turn on the power supply. The LED turns on when the PIR sensor detects motion, and turns off again shortly after. The current draw at 4,5V was about 110mA with the LED on, and 0mA while off. This of course is raw numbers read of the power supply, but this kind of project doesn’t really need 100% exact measurements.

One problem discovered was the ON-time for the LED. The PIR sensor got two pot-meters that I believe should control sensitivity and time ON. The sensitivity one seems to do the job at any setting. The timer function though seems to be “on for a second” or “on forever”. I will need to test this a bit further, or test with the other PIR sensor in case this one is flawed. Either way, as long as it is motion in from of the sensor the light will stay on.

Next up is assembling the thing, and testing it in the hallway at night.

 

F.A.I.L.

Just a small update on the project. Most DIY’ers would probably have finished a dozen weather stations by this time, lucky for me it is not a race!

Some of the reason things take time is distractions. I can search around for solutions for my current project, stumble upon a exciting new project and de-rail completely. Another reason for the long time is when my planned solutions simply just fail. According to the internet FAIL just means “first attempt in learning”. Well, with the ESP8266 I should be about to NAIL it. That means “Nth attempt in learning”.

This doesnt look like a Python prompt to me. The latter part of the screen kept looping forever…

Trying to get MicroPython running on the thing is the easiest thing in the world. Thats what the internet says. I have spent many hours trying to follow every guide out there and not getting the same end result. That means that at the moment I am at square one.

So instead of running MicroPython I will give Zerynth a go. This is Python packed in a virtual machine, ready to run on the ESP8266. I gave it a try with some sample code, and it worked straight out of the box. It’s not the real thing, and a bit less DIY than intended, but for a first edition of the weather station I guess it will suffice.

3 different ESP8266 kits. Upper right was the first one. After hours of troubleshooting I managed to flash it. It has been dead ever since. Upper left is the one I am currently fiddling with. No luck at flashing that one with MicroPython either. The bottom one is the one planned to go into the real projects. It is not very breadboard friendly, so I have not tried getting it to work. Yet.
3 different ESP8266 kits. Upper right was the first one. After hours of troubleshooting I managed to flash it. It has been dead ever since. Upper left is the one I am currently fiddling with. No luck at flashing that one with MicroPython either. The bottom one is the one planned to go into the real projects. It is not very breadboard friendly, so I have not tried getting it to work. Yet.

I also looked around to find some way to display and/or store the data. Making a database is easy enough I guess, but there are a ton of ready made web apps out there. So why reinvent the wheel? Thingspeak is actually a free service offering (I think) what I am looking for, and even better: It is open source! This means I will be able to run it on my base station for the weather station.

To wrap it up: The project is not dead. It’s just slow. Also, CivVI came out this weekend, and I’ll continue my project right after this next turn…

Creating a weather station – MX-FS-03V/MX-05V (Part 2)

Getting temperature readings with a weather station is fine and all. It is however impractical to go outside whenever I want to know it. Since a wire or a cable is out of the option, and quite outdated, I want a wireless transmission of the temperature data and whatever sensors I add later. Enter the MX-FS-03V / MX-05V.

Wireless transmitter / reciever (MX-FS-03V / MX-05V)

Roaming the wast market that is eBay I found the MX-FS-03V / MX-05V set. They are cheap and small wireless transmitters / recievers, perfect for the Arduino. I haven’t tried breaking them apart, so I am not familiar with the technical specifications other than that they work at a frequency of 433Mhz. I also know that there are some good libraries for them for the Arduino. I’m using the VirtualWire library found here.

Sending data

This is the transmitter part, MX-FS-03V. No potato for scale...
This is the transmitter part, MX-FS-03V. No potato for scale…

The VirtualWire library is designed for this kind of stuff. Using some sample code I found, and modifying it to send the temperature reading, the sending part was very easy.

This is a very early version of the code, and probably full of unnecessary stuff. Later versions will hopefully have some optimization, and a cleaner code. Basicly what the code does is reading the temperature as in the previous post. Then it converts the temperature number to a string. I assume the library can send numbers just as well, as it sends bit by bit, but with the sample code I could not figure it out. Converting it to a string was easier. It then send the string, and loops. It also strips the string of any leading or trailing whitespace. This corrected an early bug where only a part of the temperature was sent.

This sends the temperature about once every second. The selected transmission speed is 2KB per second, which is way faster than needed. But hey, if it works!

Receiving data

The receiving part.
The receiving part, MX-05V.

The receiving Arduino is, for now, a much more simple code. The only task of this code is to receive the data and print it to the serial monitor.

Both codes are based partially or fully on code I found here. They are also most likely nothing like the end result. Part of the project is to practice programming, so code will be rewritten to better fit the task.

Range

The internet disagrees strongly about the range of these devices. Some list hundreds of meters, others talk about centimeters. On my test bench they are positioned about 10cm apart, thus I have not really tested the range. Both units have the possibility to solder on an antenna, which probably will add to the range. This will be better tested later on.

Its not transmitting across any oceans, but at least it works on my test bench.
Its not transmitting across any oceans, but at least it works on my test bench.

Other possible shortcomings

The two Arduinos have been running for a couple of days now, on my test bench. One of the problems I have been having is that it suddenly stops recieving. The serial monitor prints message recieved, the message however seems incomplete. Not sure what this is about. A simple reset of the receiving device solves it, so I’m sure there is a simple workaround if this turns out to be a problem.

I have not tested the power consumption. This is mostly a potential problem for the transmitter, as it will be battery powered. I am not sure my cheap china type multimeter is capable of measuring the current draw, but this will be tested later on.

Next up!

The next part is getting the Ardiuno to talk to a Raspberry Pi. Why a RPi? This is where all the number crunching stuff will happen. I can run a web server on it, and it runs Python, which I find a lot easier than C/C++.

I also need to add more sensors, to get a complete weather station. An eBay shopping round is imminent!

My test bench. Currently sporting an Arduino, a fakeDuino, a
My test bench. Currently sporting an Arduino, a fakeDuino, a DS18B20 temperature sensor, a MX-FS-03V transmitter and a MX-05V receiver.

Creating a weather station – DS18B20 (Part 1)

Greetings world!

This will be my first attempt at writing a technical blog. I have a lot of ideas lying around, and hope this blog can inspire both myself and someone else to get creative and do stuff.

So my first post is part 1 of creating a weather station. I already have a bought one in my kitchen, but this project I think is perfect for learning more about the Arduino Uno and C programming, and further on Raspberry Pi with Python programming, as well as some databases and other cool stuff I have planned. So without further ado; Creating a weather station (Part 1):

Getting the temperature

The core function of any weather station is reading the temperature. Luckily we have come a bit further than reading the data off an old school mercury filled glass tube. Today we got electronics! After roaming the internet for a while I found that the DS18B20 is a well supported, well documented and fairly cheap component, perfect for the cause.

Connecting the DS18B20

Due to the vast amount of information about this component on the internet, connecting it to the Arduino was a breeze.

Connecting the DS18B20 to the Arduino requires 4 cables, a 4.7k resistor, the Arduino board and of course the DS18B20 itself.
Connecting the DS18B20 to the Arduino requires 4 wires, a 4.7k resistor, the Arduino board and off course the DS18B20 itself.

Red wire is power (5V), black wire is ground and white wire is the data wire.

Programming the Arduino to read from the DS18B20

The programming required to read any data of the DS18B20 is pretty ugly, at least for a rookie like my self. Luckily there is a heap of programmers better suited than me to write these kinds of programs, and since programmers are cool people they often share they work. Reading data of the One-wire protocol is easy thanks to two libraries:

The OneWire library is for reading the raw data itself. Its mentioned on the Arduino web site, and can be found here. This library can be used for any device that uses the One-wire protocol.

The data from the OneWire library is of no use in its current form for the end user. Hex data tells me very little about the weather or the temperature. The Arduino library database has a library called DallasTemperature, which is perfect for making the data nice and pretty.

Having installed these two libraries, I used the sample sketch to produce a readable temperature output.

This code outputs the current temperature. I have not dug into the library code to see what it actually does, thus I do not know how often it updates the temperature. It does seem to check about once every second, and for my use that is more than enough.

Requesting temperatures…DONE
Temperature for Device 1 is: 20.12

So there it is, 20.12°C on my workbench at the moment. The accuracy is ±0.5°C, but I can actually see the temperature change by breathing in the sensors direction.

The circuit setup for the DS18B20 temperature sensor.
The circuit setup for the DS18B20 temperature sensor.