Skip to main content

PiAMI HEAT



There is one little thing that has started to bother us.  All these unspectacular robot enhancements, all these limited robot movements, is bound to result in something.  Something dark and sinister.  Is that something "singularity", in which autonomous vacuum cleaners surpass the intelligence of human beings and end up hoovering us all up?  Doubtful.  But, heat.  Definitely.

Everyone knows that machines get hot (and not in the look-at-me-on-Instagram sort of way).  And Rosie 2.0 is no different.  When all her electronics and motors are in full flow, we need some way to cool her down.  She just needs to - quite literally - chill out.

And how hard could that task possibly be?  Are we about to make a mountain out of a mole hill?

Let's get our hands on a temperature sensor and find out the answer for ourselves.  And let's be quick.  Because we might just have to build ourselves a bunker in our backyard to escape from the marauding terror twins that will be Henry and Hetty.

You don't want to be the tool without the tooling:

  • A Raspberry Pi 3, running Raspbian OS.
  • DS18B20 1-Wire Digital Temperature Sensor.  We are using a module by Kuman, which came as part of the Kuman 37 Sensor Module Robot Starter KitDS18B20 itself is a widely used temperature sensor so it is available from many vendors and outlets, often as a module under different brand names.  Getting it as a module, as opposed to just the sensor itself, may mean you have less fiddling around to do (such as soldering a pull-up resistor).  Either way, you'll need the sensor.  Period.
  • If you want to demonstrate that some of this can actually be useful, you could hook up a cheap battery-powered fan (via a relay) to operate it.  You may also want to use a LED as a simple visual indicator.

Chequered history:

Building Rosie 2.0 - naturally - assumes that there is a Rosie 1.0.  For that reason, casting your eyes over the organised chaos that was Rosie and Rosie Patrol is highly recommended.

Bit by byte:

  • Connect the DS18B20 Digital Temperature Sensor to the GPIO pins of the Raspberry Pi
  • Write some Python code to take temperature readings
  • That's it... unless you want to also:
  • Inject a relay into a battery-powered fan, and connect it to the GPIO pins of the Raspberry Pi.  Oh yes.  Don't forget the bi-colour LED as well for the obligatory warning effect.  Remember the principle law of electronics?  Green LED = the world is a happy place (but may not be for long - see next LED colour), red LED = everything is about the be annihilated into extinction.

You ain't seen nothing yet:

1-Wire is a protocol that allows you to receive low-speed data (such as temperature measurements) using guess how many wires?  Yep.  Ninety-two.  One.  So a temperature sensor, such as the DS18B20, can make its measurements available to the Pi using only one 1-Wire-enabled GPIO pin (pin 7 / BCM 4).  And it does this extremely well.

By enabling 1-Wire in Raspbian OS, and connecting the DS18B20, the Raspbian Linux OS itself can start to read the measurements that miraculously appear visible to the filesystem... quite literally, as contents of a file.  And as a result, Python can interrogate the file to obtain the latest temperature recordings.  It really isn't very sophisticated, but unusually effective for very low-tech sensors such as one that only needs to send temperature measurements every now and again.

And what should we do with these measurements?

You could record the temperature readings inside your favourite mole mound over time, and Tweet the 3-day moving averages to www.whatistheaveragetemperatureofyourfavouritemolemound.com*.  Or alternatively, direct them at Countryfile, who are likely to happily feature your findings on their next show.  Or, you could simply operate a fan and a bi-colour (green / red) LED based on the readings.  Yes, we've opted for the most boring of the options, as we attempt to remain sensible for a change.

*Not an actual website.  But please do not attempt to visit (in case it turns out to be).

Detail:

There really isn't much to this outing; especially since taking temperature measurements using a DS18B20 digital temperature sensor and a Raspberry Pi appears to be a really popular thing to be doing.  Therefore, there is already a lot of useful tutorials out there on this very topic.

We found the below two particularly useful.
It's worth noting that we used a DS18B20 "module" - which is essentially a sensor pre-soldered into a board.  More importantly, it already has a pull-up resistor built-in, and there is a handy light that flashes on and off (presumably when it makes a reading, although we're not 100% sure).  If you are using only the DS18B20 sensor, there are some additional considerations to make (like the pull-up resistor), which the above guides carefully walk you through.

The DS18B20 essentially has three pins, and this is how we cabled them up:

DS18B20 PinPi GPIO PinDescription
GroundAny 0V (for example pin 9)0V (Ground)
DQ (Data)Pin 7 (BCM 4)This is used by 1-Wire for sending back temperature data
Vdd3.3V (pin 1)Used to power the sensor using 3.3V

Once it's all cabled up, it will probably look a little like this:


There's nothing more to this in terms of hardware, so let's quickly move on before we start to ask awkward questions, like: are there now more moles living in the UK than people?

In order to use the 1-Wire protocol, it needs to be enabled in Raspbian OS.  And the way to do this is to append a parameter in the /boot/config.txt file.  But that was blindingly obvious, right?  Not.

Open up /boot/config.txt using the nano editor.

sudo nano /boot/config.txt

And add the following line right at the end (also add a comment if it helps you remember what you did):

dtoverlay=w1-gpio


Once you save the file, it's time to reboot the Pi for the setting to take effect.

sudo reboot

Now that 1-Wire has been enabled, sensors connected to the Pi's 1-Wire pin (pin 7 / BCM 4) should be picked up.  This is how we can check this.

sudo modprobe w1-gpio
sudo modprobe w1-therm
cd /sys/bus/w1/devices
ls


The terribly named "28-011513b6e9ee" is our temperature sensor.  And we can quite literally address it using that name.  For example, navigating to its directory and opening the "w1_slave" file allows you to interrogate the sensor data in its raw format.

cd 28-011513b6e9ee
ls
cat w1_slave


In amongst the jumbled up data is actually the temperature reading.  Notice the "t=" at the end?  Divide that value by 1000, and you get your latest temperature measurement from the sensor in Celsius.


Now clearly we want to be able to get this value programmatically from within Python.

The examples that are provided by ModMyPi and The Pi Hut tell you all that you need to know.  But in short, the code simply reads the file that we were viewing not so long ago, and identifies and returns the "t=" value that we are interested in.  We'll make this module-like, so we can call get_temp() from outside this snippet, to make use of the temperature reading elsewhere.

Our code ended up looking a bit like this:

Let's now test this using IPython.

ipython3

While in the directory with the Python file, import our module, and run the get_temp() function.

import rosie_temperature
rosie_temperature.get_temp()


This is what we observe every time we run rosie_temperature.get_temp().  In this example, we were pressing against the sensor with our finger, which is why the temperature was creeping up ever so slightly between readings.

There really isn't much else to do to test that the sensor works.

However, in the interest of proving that this *could* be useful, we've hooked the Pi up to a small, battery-powered fan (via a relay), and a bi-colour (red / green) LED.  Both items were also found in the Kuman 37 Sensor Module Robot Starter Kit.  The fan, on the other hand, is a cheap, desktop fan available from many online retailers.


We've simply soldered the cables in parallel to the Normally Open (Push to Close) button equipped on the face of the fan chassis, similarly to what we did to a flashlight sometime ago.  This way, the physical switch continues to work, but the relay can also perform the identical function, electronically.

It's important to revisit the past post on operating a relay, and re-read all the accompanying warnings.  In particular, we are using a battery-operated fan on purpose, due to the low voltages involved.  Do not go anywhere near a mains-operated fan.

Here's how we cabled up the relay.

Relay PinPi GPIO PinDescription
GroundAny 0V (for example pin 6)0V (Ground)
Vcc5VUsed to power the relay
VinPin 8 (BCM 14)Used to operate the relay
CommonN/A (Fan)For closing and opening the circuit like a switch.  Connect to one end of where the circuit needs to be closed.
Normally OpenN/A (Fan)For closing and opening the circuit like a switch.  Connect to the other end of where the circuit needs to be closed.

The bi-colour LED has a much simpler set of cabling requirements.  It works by displaying a different colour depending on the direction of the current.

Normally, like the temperature sensor, you'd need to solder a resistor to protect the LED, but here, we're using a module again with a pre-cabled resistor.  This means, there's very little for us to do.

Bi-colour LED PinPi GPIO PinDescription
GroundAny 0V (for example pin 14)0V (Ground)
V (Green)Pin 10 (BCM 15)When this is high, and other is low, LED will be green
V (Red)Pin 12 (BCM 18)When this is high, and other is low, LED will be red

You can guess where we are heading with this, now that we have both a fan and a bi-colour LED under our control.

All we want to do now in our main() function is to monitor the temperature every second, then if it's above a preset warning threshold (25c in our case), we'll turn the LED red from green, and switch on the fan at the highest setting.  Once the temperature falls under that threshold, we'll turn the LED back to green, and switch off the fan.

Note that our particular fan has multiple settings which can be toggled through each push of the Normally Open switch.  This needs to be mimicked by the Python code.

The fan settings are:
  • 0 = Off
  • 1 = Low
  • 2 = Medium
  • 3 = High
  • 4 = Just the LED on (no idea why this is useful on a fan)
For all of this we've created another module - rosie_fan.py - in which we're housing two classes; one for the LED, other for the fan.  The LED class simply allows us to toggle the relevant GPIO pins to turn the LED green (set_green() method) and red (set_red() method).  Nice and simple.

The Fan class does something similar with the relay GPIO pin, but with a bit more context about what it is that it's been asked to do.  We'll track what we think is the current state of the fan (using self.fan_state) to make sure that we can instruct it to arrive at the requested target_state through the relevant number of close / open relay actions.  Of course, there's no way to keep this in sync.  If we manually push the button on the fan, the code won't know anything about this.

Here's our rosie_fan.py which we import from rosie_temperature.py:


Run rosie_temperature.py and hopefully you will see the temperature readings, and more interestingly, the fan turning on, and green LED turning red, when the threshold is reached.

python3 rosie_temperature.py


And here's what happens when we hover the temperature sensor above some hot water.


And that's about it.  Nothing more to see here.  Move along, please.

We've demonstrated that we can use a temperature sensor with the Pi and Python to take temperature readings.  And made it slightly more useful by operating a LED and a fan.  Clearly, you could do a lot more with this.  Like operate the fan in different fan settings depending on the temperature, or maybe even display the current readings on a LED display, purely for the benefit of the moles in the mounds, you understand.

But it's getting late; and quite frankly, we really need our sleep.  Besides, mole mounds are harder to find in the dark.  Ah ok, maybe that was what setting (4) on the fan was for...

Go and see the doc:

The official datasheet for the DS18B20 sensor can be found here:
There's a detailed explanation on how to get the DS18B20 working with your Pi here:
...And also here:
Finally, as always, the Pi pinout reference is very useful when trying to figure out what to connect where.  Specifically, relevant for this, is the 1-Wire pin.

Comments

Popular posts from this blog

Tea minus 30

We're fast approaching Christmas time.  And if robots were to make one simple observation about the human species during the Christmas festivities, it's that they watch a lot of TV.  A LOT.  Often, accompanied by an inappropriate amount of greenhouse gas-producing food.  Stuff you don't normally eat during the remainder of the year - for good reason.

And most so-called shows on TV are boring to robots like Rosie.  After all, why watch a minor subspecies of the human race - celebrities - stumble awkwardly around the dance floor, dressed like a faulty, sparking circuit board?  Such branch of entertainment doesn't require robots to engage any of their proud circuitry.  Their processors remain idle.  Memory under-utilised.

But if robots are to be part of people's homes (and blend in), they need to look at least a little interested in some of this irrational nonsense.  Nobody likes a party pooper.  A killjoy.  And this is where a certain subgenre of TV entertainment co…

Beam me up, Rosie!

How do you get from A to B?

You can't, as As and Bs are just letters in the alphabet. But if A is your house, and B is a meerkat village at your favourite safari park, you'd probably use a device equipped with GPS.  Not to be confused with UPS, who will deliver you your chosen meerkat through the post. And why on Earth would Rosie Patrol need one? Precisely, it's because she is on Earth that she needs one. Because our planet is rather big. Big enough to get lost in. And we don't want to lose our friendly plastic boxes on wheels. And maybe, eventually when she's clever enough, she'll go and defeat baddies on her own. And return home afterwards for a well deserved Earl Grey tea.

Besides, why wouldn't we want to add another three letter acronym to Rosie Patrol's repertoire?
All superheroes need:One Raspberry Pi 3, running Raspbian OSComputer from which you are connecting to the Raspberry Pi Probably the most important bit: a GPS receiver thingmy. …

Hello supervision, goodbye supervision

The time has finally come, to let our precious Rosie go.  Wish her luck (yes, she'll really need it).  Press the big red button.  And watch her destroy / navigate (delete as appropriate) the terrain*.  All on her own.  Without your help.  Well, sort of.

*Somewhat scientific term... for your lounge

You see, cool robots are never meant to be controlled by us clumsy humans.  If they want to be clumsy, they can be clumsy on their own.  That way we can deny all responsibility when they crash into the neighbour's car.  Or, less importantly, take over the world.

And by the way, Autonomous stuff seems to be in fashion these days.  Self-driving cars are just around the corner (maybe quite literally).  Very clever computers also beat humans at chess, apparently.  After all, if robots only do what we tell them to do, they are going to need us.  They're going to be just like us.  Oh no.  We want them to work alongside us, to do things that we simply can't be bothered to do.  Like …