3

I am using mpc3008 in order to convert the analogue signal of two sensors, for a rpi3.

The sensors are, the SEN0189 turbidity sensor and the SEN0244 TDS sensor.

The code I am using is the following:

import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008
import time

SPI_PORT   = 0
SPI_DEVICE = 0
mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))

print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |'.format(*range(8)))
print('-' * 57)

while True:
    values = [0]*8
    for i in range(8):
        values[i] = mcp.read_adc(i)
    print('| {0:>4} | {1:>4} | {2:>4} | {3:>4} | {4:>4} | {5:>4} | {6:>4} | {7:>4} |'.format(*values))
    time.sleep(0.5)

And the fluctuation I am observing in the unused channel 2-7 is the following.

And I am wondering if this is normal, or if it is some kind of noise due to the cobbler, the breadboard, or the adc itself. If it is normal then how is it explained?

|    0 |    1 |    2 |    3 |    4 |    5 |    6 |    7 |
---------------------------------------------------------
|   26 |  466 |   11 |   14 |   15 |   18 |   22 |   40 |
|   26 |  466 |  205 |  215 |  214 |  216 |  219 |  225 |
|   26 |  463 |  260 |  218 |  221 |  220 |  222 |  223 |
|   26 |  463 |  252 |  201 |  197 |  199 |  201 |  192 |
|   25 |  462 |  251 |  199 |  194 |  192 |  185 |  164 |
|   26 |  463 |  240 |  184 |  176 |  174 |  169 |  139 |
|   25 |  462 |  227 |  169 |  158 |  156 |  148 |  106 |
|   25 |  463 |  208 |  145 |  126 |  103 |   71 |    0 |
|   26 |  463 |   54 |    0 |    0 |    0 |    0 |    0 |
|   25 |  462 |    0 |    0 |    0 |    0 |    0 |    0 |
|   25 |  463 |    0 |    0 |    0 |    0 |    0 |    0 |
|   25 |  463 |    0 |    3 |    4 |    4 |    7 |   16 |
|   25 |  462 |   11 |   15 |   16 |   20 |   26 |   48 |
|   25 |  463 |   26 |   31 |   32 |   41 |   54 |  114 |
|   25 |  463 |  157 |  178 |  196 |  219 |  230 |  241 |
|   26 |  463 |  261 |  216 |  218 |  219 |  221 |  228 |
|   26 |  463 |  253 |  202 |  197 |  199 |  200 |  191 |
|   26 |  468 |  251 |  196 |  187 |  184 |  181 |  159 |
|   26 |  463 |  236 |  179 |  171 |  169 |  162 |  123 |
|   26 |  463 |  222 |  164 |  156 |  151 |  140 |   93 |
|   26 |  463 |  163 |   91 |   68 |   47 |   24 |    0 |
|   26 |  462 |    0 |    0 |    0 |    0 |    0 |    0 |
|   25 |  463 |    0 |    0 |    0 |    0 |    0 |    0 |
|   25 |  463 |    0 |    0 |    0 |    0 |    0 |    0 |
|   25 |  463 |    3 |    6 |    7 |    9 |   10 |   25 |
|   25 |  462 |   15 |   19 |   22 |   25 |   30 |   61 |
|   25 |  463 |   41 |   58 |   74 |   97 |  134 |  246 |
|   25 |  463 |  217 |  228 |  224 |  225 |  229 |  235 |
|   26 |  463 |  258 |  214 |  214 |  216 |  215 |  211 |
|   26 |  462 |  252 |  201 |  197 |  198 |  200 |  191 |
|   25 |  462 |  245 |  190 |  182 |  182 |  176 |  157 |
|   26 |  463 |  234 |  178 |  171 |  167 |  162 |  127 |
|   26 |  463 |  221 |  163 |  154 |  149 |  135 |   91 |
|   26 |  463 |  127 |   56 |   34 |   12 |    0 |    0 |
|   25 |  463 |    0 |    0 |    0 |    0 |    0 |    0 |
|   25 |  463 |    0 |    0 |    0 |    0 |    0 |    0 |
|   26 |  463 |    0 |    0 |    0 |    0 |    0 |    5 |
|   25 |  463 |    5 |    8 |    9 |   10 |   13 |   29 |
|   25 |  463 |   16 |   20 |   22 |   26 |   33 |   60 |
|   26 |  463 |   45 |   62 |   80 |  103 |  143 |  250 |
|   26 |  463 |  212 |  227 |  224 |  227 |  228 |  235 |
|   26 |  463 |  259 |  213 |  215 |  217 |  219 |  214 |
|   26 |  463 |  252 |  201 |  197 |  198 |  200 |  191 |
|   25 |  463 |  246 |  191 |  184 |  182 |  179 |  156 |
|   25 |  463 |  233 |  177 |  169 |  163 |  159 |  124 |
|   26 |  463 |  216 |  158 |  150 |  142 |  131 |   76 |
|   26 |  463 |  108 |   36 |   13 |    0 |    0 |    0 |
|   25 |  463 |    0 |    0 |    0 |    0 |    0 |    0 |
|   25 |  463 |    0 |    0 |    0 |    0 |    0 |    0 |
|   25 |  463 |    0 |    0 |    0 |    0 |    0 |    0 |
|   25 |  463 |    4 |    6 |    8 |   10 |   12 |   27 |
|   26 |  463 |   16 |   19 |   22 |   25 |   33 |   60 |
2

This is normal. ADC inputs are made very sensitive on purpose, and will work as antennas picking up all kinds of noise when not connected to anything. Periodic noise can usually be traced to your mains voltage, which you can confirm by sampling the pin 200..500 times per second.

4
  • Is there a way to calibrate the sampling rate when using the Adafruit_GPIO.SPI library?
    – user94361
    Nov 30 '18 at 20:55
  • @ChristosK. Sorry, I have no experience with it. See how fast you can get if you remove the sleep line from your code. Dec 3 '18 at 8:10
  • A nice complementary question would be if that noise contributes at all to the value measured when a sensor is connected, or if it's totally overshadowed by the signal of the sensor.
    – user94361
    Dec 15 '18 at 15:46
  • 1
    @ChristosK. Probably the latter. Measure the output resistance of the sensor (or find it in the datasheet), then connect the resistor of that value between the analog input and ground and see how much noise remains. Dec 17 '18 at 8:52
4

I assume you have not connected anything to those inputs. The inputs will therefore be floating and return random results.

It's probably simplest to connect unused inputs to ground if you do not like this behaviour.

3
  • 1
    Thanks for the hint! I will try to ground my free inputs. However, as you can see, the values may be floating but they are not entirely random, there is a wave-like pattern.
    – user94361
    Nov 29 '18 at 20:54
  • 1
    @ChristosK. Yes, an interesting pattern. You'd need someone with electronics experience to give the reasons. They will disappear once connect the inputs to a fixed voltage.
    – joan
    Nov 29 '18 at 21:16
  • You were right about the behavior of the free channels when you ground them! They indeed stop this fluctuation. However, I think I should mark Dmitry's answer as correct because it's more explanatory according to my question.
    – user94361
    Nov 30 '18 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy