Sniff is a "Scratch-like" programming language that's designed to help Scratchers move gently from Scratch to more conventional languages. They can start writing programs, without having to learn a new language because Sniff is based on Scratch. They learn a little more about variables, compiling, syntax errors (!), and they can have fun controlling real hardware while they're doing it.

Friday, 20 February 2015

Wedo Thermometer

Minecraft has been a lot of fun the last week to so, and I'll be back with more of it next week, but I thought it was time to get back to basics, play with some Lego, and do some science!

The Wedo has amazing untapped potential as a simple and convenient way of connecting computers and Lego. It's based on the Lego Power Functions system which allows it to drive motors, and lights, but it also can use special Wedo sensors. Unfortunately Lego only make two sensors: the distance sensor and the tilt sensor. While that's a great start they're really missing out on what could be an amazing science tool, if only more stuff could be hooked up easily.

Well as it turns out, it can... provided you're prepared to do a little DIY work.The wired of the Wedo cable are (from bottom to top) 0V, C1, C2, and 9V. When driving a motor C1 and C2 are used to provide power to the motor, while sensors (and the IR remote) draw power from the 0/9V lines, and signal the Wedo hub by applying a voltage to C1 and C2. The C1 wire is used to tell the software what kind of thing is attached, while the C2 line is used to provide the reading. The hub converts both voltages to a number between 0 and 255, and sends this over USB to the computer.

Sniff doesn't really use the C1 line, as it doesn't auto detect like the lego software does (though you can use the wedoScan command to read that value and check whats plugged in). If we can generate a suitable voltage and apply it to C2 we can hook anything we like to the Wedo!

One of the things I keep coming back to over and over is measuring temperature. If we could use this approach to make a Wedo thermometer we could start doing more science with lego. And it turns out to be really cheap and easy.

Ingredients:

1xLego Power Functions extension cable #8886(£2.99 from Lego)
1x10K NTC Thermister (about £1 on ebay - treat yourself to the waterproof version)
1x10K resistor (about £0.02)
1xcup of hot water (available around the home)
1xcup of ice (available around the home)
1xglass of red wine (available around the home)
A regular thermometer

Cut the power functions cable about an inch from the end THAT DOESNT PLUG INTO THE WEDO. This might be handy for something, so don't cut that end too short, but we can put the short end in the spares box for now, and keep the cable that fits the Wedo hub.

Laying it flat with the bare end of the cable to the right, solder the 10K resistor between the bottom wire (earth) and third wire (C2). Wire the Thermistor between the third wire and the 4th/top wire (9v).

Check nothing is shorting out, and plug into the wedo. It should report that nothing is connected, but it you read a value back you should see you're getting a reading that varies with temperature. I added a readRaw function to the Sniff wedo device to work with unofficial hardware, so I could write the code:

make wedo device
make wedoConnector number
make wedoValue number

when start
.forever
..set wedoConnector to 2
..tell wedo to "readRaw"
..say [wedoValue]

..wait 0.5 secs

(this won't compile in Sniff 14 - you'll need to wait for 15). Warming and cooling the device by just holding it tightly shows that the value changes with temperature.

Now drink the glass of red wine. This stage is strictly optional, but highly recommended (unless you're under 18 or live in the USA, in which case... you know... drugs are bad).

Now we need to calibrate it.


I used a ds18 thermometer connected to an Arduino and put both the new sensor and the ds18 into first ice and then a cup of hot water, recording the values on each.

0.87=50.25deg
0.5607=1.13deg

Now we just need to fit a straight line through that (these things are supposed to be pretty linear, but we'll see!).

50.25 =0.87 *m+c
1.13= 0.56*m+c

Subtract:

49.12 1=0.31*m

m=49.12/0.31

m=158.4

Substitute into either equation:

c=50.25-0.87*158.4

c=-87.56

So to convert from a reading to a temperature we multiply by 158.4 then subtract 87.6.

Just sitting on the bench I got a reading of 0.67, which would be 18.5 degrees - about right. 


The only thing left to do is revisit the coffee going cold experiment. There are a couple of versions of coffee.sniff included in the current Sniff release, and its one of the very first science experiments I did with Sniff. I hooked up a ds18 thermometer to an Arduino, attached a tft screen, and plotted a graph as the sensor recorded the temperature of my coffee as it went cold. I later repeated it with a Raspberry Pi.

I reworked the code to use the Wedo sensor, and plot the results in a window. The code is 99% the same as before.

One of the first things that becomes clear is that we're never going to get a lot of accuracy - the wedo is an 8 bit analog to digital converter, which in this case means we're going to only get readings in about 0.5 degree steps, even if our calibration is spot on.

That means we get gaps in the graph, so it doesn't look quite so pretty as the ds18. However it does show the slight curve we expect from this experiment.  We could probably tweak the components to get a slightly better accuracy, but realistically we need to treat the Wedo as what it is - a fun and easy way to hook things up and try things out - not a precision instrument. It would be perfectly good for tracking the temperature of the classroom to see show it getting hotter and colder throughout the day.

We can hook a light sensor up in pretty much the same way... More on that another time.



No comments:

Post a Comment