Public Lab Research note

Testing of DF Robot Turbidity Sensor

by wmacfarl | October 08, 2019 15:48 | 59 views | 0 comments | #21114 | 59 views | 0 comments | #21114 08 Oct 15:48

This is part of a series of notes that I'm putting together for basic setup and testing of a suite of Arduino-based water quality sensors through the process of connecting the sensor to an Arduino, getting data from the sensor to your computer, and calibrating and making sense of the incoming data.

This series will not address any issues around logging data or deploying these sensors in the field. These issues should be addressed but this series is going to be about the basic process of connecting a sensor to an Arduino and seeing sensor-data on your computer screen.

While this series is specific to the set of Arduino-based water quality sensors that I am currently investigating, a lot of the process is generalizable to other sensors and other microcontroller platforms.

Required Materials

DFRobot Turbidity Sensor ($9.90 from DFRobot)

This is the sensor that we are going to be using to collect data. The sensor itself is a small plastic enclosure with a light-source and light-sensor. The sensor shines light out from the light source. This light travels through a small quantity of the liquid you are measuring. The amount of light that makes it through the liquid to the light-sensor gives us a measurement of how opaque the water is.

The sensor plugs into a small board with a circuit. This board can be connected to your Arduino and simplifies the process of getting data from the sensor.

An Arduino Uno or Arduino Nano ($22.00 from Arduino, cheaper clone boards are available on Amazon (Uno, Nano), Ebay (Uno, Nano), and other resellers.

If you are using one of the cheaper Arduino clones you will probably have to install special drivers to make them work. These drivers can be downloaded here.

This is the microcontroller that we will be connecting our sensor to. The microcontroller will serve as the connection between our computer and the sensor. We will be programming the Arduino to take readings from our sensor and send them to our computer over a USB connection.

There are lots of different kinds of microcontrollers that you can use to get data from sensors. One of the important differences between these microcontrollers is whether they use 5 Volt or 3.3 Volt logic. Ideally you want to choose a microcontroller that uses the same voltage as your sensor for its logic.

The DFRobot sensors use 5V, as do the Arduino Uno+Nano. Many newer microcontrollers use 3.3V. While it is possible to use a 5V sensor with a 3.3V microcontroller (or vice versa), sometimes this will require more work or a more complicated circuit to make sure you are getting accurate data and not damaging your equipment.

Hook-Up Wires

These hook-up wires are designed to be easy to connect to a breadboard and to the sockets for making connections to the Arduino and the turbidity sensor board.

Breadboard (if using the Arduino Nano)

While the Arduino Uno has a set of sockets that you can insert wires into in order to make connections to the board, the Arduino Nano has "header pin" connectors. We will use a small breadboard to make connections between our hook-up wires and these header pins.

A USB Cable

This cable will connect your Arduino to your computer. We will use this connection to program the Arduino and to allow the Arduino to send data from our sensor back to our computer. If you are using an Arduino Nano you probably need a USB mini-b cable and if you are using an Uno you probably need a usb-b cable.

A Computer

You will need a computer to power, program, and read data from the sensor.

The Arduino IDE that we will be using runs on Mac, Windows, and Linux. Chromebook users can use the Arduino Create app to program the Arduino but it has a small subscription fee.

It is possible to set up the circuit to be powered from its own power source and save its data to a separate storage device so you don't need to bring a computer to the field but I will not cover that in this guide.

Testing Your Arduino

Before we do anything with our sensor, we want to test our Arduino and make sure that we can write code from our computer to the Arduino and get data from the Arduino back to the computer.

Doing this setup and these simple tests before we build our circuit and connect our sensor will simplify any troubleshooting we have to do down the road.

Step 1: Download+Install Arduino Software

We will use the Arduino IDE to program our Arduino and to read data from it. You can download it here.

Installation instructions can be found on the Arduino website for Windows, Mac, and Linux.

If you are using one of the cheaper clone Arduino boards you will probably also need to install the CH430 drivers. You can find them here.

Step 2: Connect Arduino to Computer by USB

The Arduino uses a USB cable to connect to your PC. We will use this USB connection to send the program that we want the Arduino to run from our computer and to allow the Arduino to send data back to our computer.

The Nano and the Uno have different form-factors and use different styles of USB cable but they both have a built-in USB port for programming and charging that you can plug a cable into.

Step 3: Upload and run a test program

The next thing we want to do is to upload and run an example program from the Arduino IDE so that we can see that the board is working and our program is running.

The Arduino website has instructions to do this for the Uno and the Nano.

If you are having trouble getting your first test program uploaded to the board, the Arduino webpage has a long troubleshooting section that you can look at. The most likely cause of trouble, though, is that your computer just isn't seeing the Arduino through the USB port.

You may get an error message when you press the upload button (usually it will say something like "An error occurred while uploading the sketch" or "Problem uploading to board.")

image description

If you get an error like this, you will probably solve it by performing the following steps:

  1. Check to make sure that your Arduino has power. There should be a power indicator light on the board that should be ON. If it is not on that means your board isn't getting power over USB. Try switching USB cables (because the cable might be broken) and try all of the different USB ports on your computer.
  2. If the board is getting power but you still have an error message, you probably don't have the correct port selected. Go to the Tools->Port menu and try to switch to a different COM port. Try them all and try uploading your sketch with each one.
  3. If your board is getting power and you've tried all of your available ports and you still get these errors when you try to upload your sketch, you should try unplugging the board and plugging it back in again, testing all the available USB ports on your computer as you do so.

This will solve the vast majority of your problems. If this doesn't help, you should have a look at the Arduino troubleshooting guide.

Connecting Your Sensor to Your Arduino

The DFRobot Turbidity sensor consists of two parts, the sensor probe and the circuit board. This can be connected to each other and to your Arduino with the included sets of wires.

image description

Step 1: Connect Sensor Probe to Circuit Board

The sensor probe can be connected to the circuit board using the included Black-Yellow-Red set of 3 wires.

These wires have a white plastic connector on both sides that will allow you to plug them into sockets on the sensor probe and the circuit board.

Start by plugging the wire into the plug on the circuit board.

image description

NOTE: These wires are NOT reversible. Make sure the correct side of the wires is connected to the circuit board.

If you are looking at the circuit board from the top with the writing right-side-up, you will plug the wire into te socket on the left side of the board.

The BLACK wire will be the top wire, with YELLOW in the middle and RED on the bottom.

The plug on the other side of the wire has a small plastic clip on top of it but is otherwise identical and will fit into this socket but the wires go RED BLACK YELLOW. This setup is incorrect.image description

Plug the other side of the wires to the socket on the sensor probe

The other side of the set of wires has a small tab on the top of the plug and has a color-order of RED-BLACK-YELLOW. Plug this side into the socket on the bottom of the sensor-probe.image description


Note: if the board and probed are arranged in this way, then the wiring should be as follows:_

  • TOP wire on board connected to the BOTTOM wire on the probe
  • MIDDLE wire on board connected to TOP wire on probe
  • BOTTOM wire on board connected to MIDDLE wire on probe

Step 2: Connect Hook-up Wires to Sensor Circuit Board

On the other side of the circuit board from the sensor connection there is another socket that you can plug your other set of wires into. These wires will allow us to connect our sensor to our Arduino.

image description

This BLACK-RED-BLUE set of wires has the plug to connect to the sensor-board on one side and a set of sockets on the other. We will use our hook-up wire to make a connection between these sockets and our Arduino.

image description

Step 3: Prepare Arduino for wire connections

Both the Arduino UNO and the Arduino NANO have two sets of connectors on opposite sides of the board. All of these connectors are labelled with labels printed on the circuit board next to them.

On the Arduino UNO these connections are a series of sockets on the top of the board that you can plug wires into:image description

On the Arduino NANO they are a series of header-pins coming out of the bottom of the board that can be plugged into other sockets:image description

Both of these kinds of connection-points are called "_pins" _when we are talking about this kind of electronics. So we always talk about making connections between _pins _on the Arduino and _pins _on the other circuit boards that we're using, regardless of what physical form these connection-points takes.

But we cannot plug wires directly into the Arduino NANO. In order to make connections to the header-pins on the NANO we will need to use another tool called a _breadboard. _

The _breadboard _has a set of connection points that we can plug wires and header-pins into in order to make connections between them. This guide will describe how to use a breadboard to build this particular circuit. Adafruit has a more in depth explanation of breadboards that you can read for more information.

There are many different size of breadboards. In this guide I am using a tiny breadboard that the Arduino NANO just barely fits on. This size is great for simple circuits with just a few components. Regardless of the size of the breadboard, it will have a dividing line separating one half from the other.

image description

You will need to attach your Arduino NANO by inserting the header pins into the sockets on the board with one row of header pins on each side of the divider.image description

You can use the sockets in the board to make connections to the pins on the NANO. To connect a wire to a certain pin, you will just plug that wire into a socket on the _same row _as the header pin that you want to make a connection to.

With the NANO plugged into the breadboard, we can use the UNO and the NANO in much the same way.

![image description](/i/35684.“imagejpg" ""Nano_and_Uno.jpg"")

Step 4: Connect Sensor to Arduino with Hook-up Wires

In this step we will connect the circuit board for our sensor to our Arduino so that we can get data readings from the sensor probe. This wiring is specific to the DF Robot turbidity sensor that we are using, but is going to be very similar for different sensors.

Any sensor that you are using will have a datasheet or a guide that tells you how to wire it up. In this case I am referring to the turbidity sensor guide on the DF Robot wiki.

There are 3 wires coming from the sensor circuit board that we need to connect to our Arduino. They are RED, BLACK, and BLUE. Each of these wires needs to be connected to a specific pin on the Arduino.

  • The RED wire is for the POWER connection. It needs to be plugged into the pin labelled 5V.
  • The BLACK wire is for the GROUND connection. It needs to plugged into one of the pins labelled GND.
  • The BLUE wire is the DATA or SIGNAL connection. It needs to be plugged into one of the _Analog Input _pins on the Arduino. We will use the pin labelled A0.image description

The RED and BLACK (POWER and GROUND) wires connect the sensor to a power supply so that it has the electricity that it needs in order to run. The BLUE (SIGNAL) wire is the wire that sends an electrical signal from the sensor corresponding to the turbidity in the liquid.

Many sensors you use will follow this pattern of having a POWER, GROUND, and SIGNAL wire and will be wired up in much the same way. The main things you have to do diffrently for different sensors are:

  • Make sure you have the right voltage (5 volts or 3.3 volts based on the sensor)
  • Understand how to interpret the signal that you get from your sensor

Step 5: Upload and Test Code

Whenever you are working with an Arduino, your project will have two parts.

  • A _hardware _portion where you build the circuit according to the specifications expected by your sensor
  • A _software _portion where you program the Arduino to do all of the things that it needs to do to take readings from your sensor.

Without software, your Arduino doesn't know what to do with your sensor and won't take any readings at all. It will just keep running the "Blink" program that we used to test whether we could get it to work at all.

The coding that we do to make the sensor work is frequently very simple and usually the maker of your sensor will provide you with code that you can copy and paste to get started. You don't have to understand every aspect of this code in order to use it, but most example code that you find will have some comments written that explain how it works.

The most important thing we need to make sure of when we are using someone else's Arduino code is that their circuit is built the same way that ours is. In particular, if we have plugged our sensor into a certain pin (in our case, A0) we need to make sure that the code is written to get data from that same pin and change the code (or the circuit) if it isn't.

Again, the turbidity sensor guide on the DF Robot wiki has sample code that we can use to get started.

void setup() { Serial.begin(9600); //Baud rate: 9600 } void loop() { int sensorValue = analogRead(A0);// read the input on analog pin 0: float voltage = sensorValue * (5.0 / 1024.0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V): Serial.println(voltage); // print out the value you read: delay(500); }

This code is pretty simple. It does just a couple of things:

  • Sets up a way to send data from the Arduino to the computer over the USB port: Serial.begin(9600)
  • Reads the a value coming in from the sensor on pin A0: sensorValue = analogRead(A0)
  • Does some math to convert that value, which comes in as a number between 0 and 1023, to a voltage for easier reading: voltage = sensorValue * (5.0 /1024.0)
  • Sends this voltage to the computer over the USB port: Serial.println(voltage)

It does that over and over again with a half-second delay between each reading.

We can copy and paste this code into our Arduino IDE and then use the "Upload" button to send it to the Arduino.

image description

If everything is working, some lights should blink on the Arduino we should see an "Upload Successful" message at the bottom of the IDE.image description

If you don't, you can refer to the "Troubleshooting" steps in the beginning of this guide that we went through when we first tested our Arduino.

Step 6: Read Sensor Values

Now that we've uploaded our code to the Arduino we want to check to see that we are getting data values back from our sensor and that these values make sense.

We can see the data being sent to our computer over the USB port using a tool in the Arduino software called the "Serial Monitor." You can access it through the "Tools" menu.

image description

The serial monitor will bring up a window that looks like this. You should see a new number between 0 and 5 appear once every half-second. This is the reading we are getting from our sensor. A higher number corresponds to more light making its way to the sensor - this means _less _turbidity.

When the sensor is just in the air you should get a relatively high value. I get around 3.5 If I put an opaque object in the sensor probe I get a number near 0.

This depicts me putting a piece of cardstock between the sensor probes and then taking it out again.image description

Next Steps

Now that we know we are getting data that makes some sense and has some relationship to the sensor, our next steps are to calibrate it more precisely and take some measurements of actual liquids. We will go over that process in another research note.


If you _aren't _getting sensible seeming data, there are a couple of likely culprits.

  • If the value you get is always "0.0", you have probably reversed the wiring plug between the circuit board and the sensor probe. See "Step 1" for more details.
  • If you are always getting a middling value that changes randomly without any regard for what is between the sensor probe, you have probably plugged the SIGNAL wire into the wrong pin of the Arduino. Make sure it is plugged into pin A0 and that the code you have uploaded to the board says **analogRead(A0) **to match.
  • If you don't get any number coming in through the Serial Monitor, you have probably not successfully uploaded your code to the Arduino. See the **Testing Your Arduino **section for more information.

If you have any other problems, _please _leave a comment on this research note. I would love to help you debug the issue and add it to my troubleshooting guide!


I did this Help out by offering feedback!

People who did this (0)

None yet. Be the first to post one!


Login to comment.

Public Lab is open for anyone and will always be free. By signing up you'll join a diverse group of community researchers and tap into a lot of grassroots expertise.

Sign up