[IoT] Aquarium monitor; hardware wiring

This entry is part 2 of 9 in the series Azure Aquarium Monitor

Hi there! This is post #2 in my Aquarium monitor series. In this post, I’ll explain how I wired the different sensors to my Raspberry Pi and Arduino devices. Now you probably do not have exactly the same set-up as I have, as there are all kinds of different devices out there which all require their own set-up / configuration. So this post is written as general guidance of how to connect sensors in general. 

The hardware

Small recap from the first post; I’ll be using a Raspberry Pi 2 running Windows 10 IoT. Linked to that will be an Arduino nano with some sensors attached to it. I recommend hackster.io for tons of good blogs detailing on how you can wire all kinds of sensors to all kinds of boards. Do some research first and order the parts that suit your situation best. In this case, I’ll be wiring:

  • 2 temperature sensors (DS18B20 onewire) to monitor water temperature
  • 1 pH sensor

Once you get the trick of reading a sensor, the rest becomes pretty trivial real fast. So feel free to slam in a couple of more if you need them.


Start small! Hello world!

Ok yes, this might be the lamest thing I could start with, but I really need to emphasize the need to start small. Once you start receiving the stuff you ordered, your hands will be itching to get it all working, like mine were too. But this set-up consists of several moving parts. And the more parts you have, the more places you create for failure to happen. So my advise would be the following:

  1. Find out whether your sensor is analog or digital. Analog sensors you will connect to the Arduino, digital ones you can connect to either the Raspberry or the Arduino. I would recommend to wire everything to the Arduino to begin with, just to have everything in one place.
  2. Find out how your sensor is wired to the device (see hackster link above) and connect it like that. Check multiple blogs when you can find ’em as I found out the information is not always 100% correct.
  3. With the sensor connected, write a little Arduino program to read out the value and print it to the serial monitor. Do this for all your sensors and be sure to save that code somewhere safe (like GitHub).
  4. Once you have all sensors reading values, now start working on getting those values to the Raspberry using I2C. That I will describe in more detail below.


Wiring the sensors

So I started hacking away. Analog and digital sensors require different ways of getting the values out of them. My temperature sensors require a 4k7 ohm “pull-up” resistor which apparently has something to do with how the signal hits the board. Again, I can’t stress enough that I am no expert in these things and you should google your ass off to try and find blogs covering your particular set-up. The good thing here is that you can use all of the Arduino blogs out there, your good as long as you can get the correct value to print out on the screen.

Neptune Sketch_bb

Above is (part of) my set-up made with a nifty little program called Fritzing. In the diagram you see the following:

  • The Raspberry is connected to a breadbord using whats called a ‘cobbler’, the blue T-shape component on the right.
  • The Arduino is the little device you see on the left.
  • Digital port 3 (D3) of the Arduino is linked to two DS18B20 sensors. Yes, two on the same port. This works beautifully without much additional effort, take a look at the code below.
  • Digital port 13 (D13) is linked to my pH sensor board (which in turn is connected with the actual sensor).
  • Ports 23 – 26 of the Raspberry are connected to the inputs of the relay board (4 channels).
  • The SDA and SCL ports of the Raspberry are the ones we can use for I2C communication. Those are linked to A4 and A5 respectively (which are SDA and SCL on the Arduino). More about I2C is found below.
  • I’ve linked the grounds of all components to create one common ground.
  • The Arduino is fed using power from the Raspberry via USB, the relay board is powered via the 5V output channel.

That’s pretty much all of the wiring, although I must mention this doesn’t feature the moisture sensor I have. As you can see, not that hard once you’ve got it figured out.


Writing the code

Okay. So now we have to write some code to get these actual values. As stated earlier; do this one sensor at a time so you know the individual parts work. Once you have all parts, combine them into one which will resemble something like you see below. In my opinion this isn’t much code so I’m just going to paste it in here. Explanation is inline and below.

I hope most of the code speaks for itself; but here are some extra pointers:

  • The one wire temperature sensors are really easy to read when you use the Dallas OneWire.h library.
  • The pH sensor board took me quite some time to get working and it’s quite possible that the same code might not work for yours. This is a cheap-ass China made one which I’ll be swapping for a better one real soon. Check out this post on stackexchange for more information on that one.


Sending information to the Raspberry via I2C

Ok, so if you’ve followed the steps, you now have Arduino code capable of reading and writing sensor values. Next step is to get these values to the Raspberry. As you’ve seen in the wiring, two ports are used for that which in turn use the I2C protocol for communicating. This is where the Wire.h library is used for so you don’t have to start writing communication protocols yourself.

The code itself consists of these parts:

This code runs as part of the setup method and registers the requestEvent method. This method will be called each time the device receives a request from the host. Inside the method, it should write it’s message payload to the wire, which you see below:

Not that complicated, right? It isn’t!

Of course this is only one side of master-slave communication. The Raspberry will run the master code and request data from the Arduino. It will now get a 12-byte array which consists of the registered temperatures (2x 4 bytes) and pH value (4 bytes).

The real cracks will most likely find all kinds of problems with this code, but at least it works! In my next blog, I’ll detail the Raspberry side of things and we’ll look at how to get some information up to the cloud. W00t!


Nice to knows

Whenever I think of things that are helpful to know, I’ll add them below. Hope it helps!

  • With wires going to the Raspberry, my laptop sometimes has trouble identifying the Arduino via USB. Temporarily disconnecting the two seems to fix this, probably some kind of current issue.
Series Navigation<< [IoT] My steps to an aquarium monitor (index)[IoT] Aquarium monitor; Raspberry & Azure IoT hub >>

2 thoughts on “[IoT] Aquarium monitor; hardware wiring

Leave a Reply

Your email address will not be published. Required fields are marked *