Unable to configure Hall effect current sensor to Raspberry Pi 3b


#1

As a final a final project me and my friend were working on a home automation and power monitoring system. We are using a MCP3204 to convert the analogue voltage output from the hall effect current sensor to get a reading on the pi. So far the relays are functional and the MCP3204 extension is connected to the pi.
Now this is where we have been stuck for past 2 weeks. The pi is not detecting the analogue input from adc.
Please help us out here as there is nothing can think if doing. We tried to used MCP3008 and we still get stuck at the same place.


Also inform us if the Signal from Hall Effect current sensor can directly be used or needs a conditioning circuit. If it is required then just tell us what it is.
We have to submit on friday morning so guys please help us…!!


#2

Hi @nihal.behal, welcome to the Cayenne Community.

Have you created any widgets associated with that Raspberry Pi to read the value on one of the channels of the MCP3204? If so, can you share what their settings look like?


#3

Yaa sure.i have created 3 genetic analogue sensors.
Each channel (0,1,2) respond to a current sensor.


#4

Could you show me what they look like on the actual dashboard? Just empty widgets with a - where there should be a value?


#5

It just showed 0 as the value


#6

I wonder if it is a wiring issue – are you able to get a reading from these sensors on your Pi’s command line, removing Cayenne from the equation for a moment?

If so, as a workaround, you could connect the Pi via the ‘Bring Your Own Thing’ MQTT API option and pass the sensor data into Cayenne via MQTT publish statements.


#7

None of us know any coding… That’s the reason we picked Cayenne in the first place…:cry:


#8

I guess the wiring is right cz we have rechecked the entire circuit more than a couple of times


#9

Could you double check that the SPI Interface is enabled on your Pi as this would be needed to read from the MCP3204 chip? If it is not, please reboot after enabling it when prompted.


#10

Check…its there


#11

Thanks,

I will look into getting you a small python script to read the inputs on the Pis command line to make sure there are actually values there to be passed into Cayenne. Since I don’t have an MCP3204 I’ll use the MCP3008. If we can see the values on the command line I can show you how to get this incorporated into Cayenne via MQTT.

The one caveat is that the Android app does not yet have MQTT support (coming at the end of this month!) so if that is crucial to your project then it might not be the right road to travel. You’d be able to see all of the data on the web dashboard or iOS.


#12

Thanks a ton i do have a mcp3008 with me…ill redo the connections…no problems…n i generally work with a ipad only…so i guess that solves another issue…thanks a lot anyways


#13

Actually we are currently a little short on time so can u please provide us the script.?


#14

I’ll have something for you during the USA day tomorrow.


#15

Life saver…:slight_smile:


#16

When can i expect the python code for adc


#17

Here is some code to read the sensor

Here is an example of uploading data to Cayenne with MQTT via python on the Pi.


#18

Thanks a lot…


#19

@nihal.behal

Thank you for your patience on this. I wanted to share what I made too, though I’m sure its very similar to what @adam linked.

First, I just wanted to make sure I could read values coming off the MCP3008 chip. For this I followed the instruction at this page: https://learn.adafruit.com/raspberry-pi-analog-to-digital-converters/mcp3008 in order to print the output for each of the 8 MCP3008 pins to my command line. If you followed the wiring instructions in the Cayenne MCP3008 tutorial, you can use the “Hardware SPI” option in that article.

pi@raspberrypi:~/Adafruit_Python_MCP3008/examples $ sudo python simpletest.py
Reading MCP3008 values, press Ctrl-C to quit...
|    0 |    1 |    2 |    3 |    4 |    5 |    6 |    7 |
---------------------------------------------------------
|  797 |  642 |  766 |   47 |   51 |   55 |   67 |  103 |
|  797 |  643 |  766 |   49 |   53 |   57 |   69 |  106 |
|  798 |  647 |  768 |   50 |   54 |   58 |   70 |  106 |
|  799 |  649 |  769 |   46 |   53 |   56 |   68 |  102 |
|  799 |  649 |  769 |   46 |   51 |   53 |   62 |   92 |
|  799 |  646 |  768 |   27 |   28 |   28 |   31 |   43 |
|  797 |  642 |  767 |    8 |    9 |    8 |    8 |   10 |
|  797 |  642 |  766 |    2 |    0 |    0 |    0 |    0 |
|  797 |  645 |  767 |    0 |    0 |    0 |    0 |    0 |
|  800 |  642 |  767 |   36 |    0 |    0 |    0 |    0 |
|  797 |  642 |  767 |    2 |    2 |    1 |    0 |    4 |
|  797 |  644 |  767 |    0 |    0 |    0 |    0 |    0 |
|  798 |  647 |  768 |    0 |    0 |    0 |    0 |    1 |
|  799 |  649 |  769 |    1 |    1 |    1 |    1 |    4 |
|  799 |  649 |  769 |    3 |    4 |    4 |    6 |   12 |
|  798 |  646 |  767 |   21 |   24 |   28 |   36 |   59 |
|  797 |  642 |  765 |   38 |   43 |   47 |   59 |   93 |
|  797 |  641 |  765 |   47 |   51 |   56 |   68 |  104 |
|  797 |  643 |  766 |   50 |   54 |   58 |   70 |  107 |
|  798 |  646 |  767 |   49 |   53 |   57 |   70 |  108 |
|  798 |  648 |  768 |   49 |   52 |   56 |   67 |  100 |

In my case, since I don’t have these Hall effect current sensors, I have 3 photoresistors on MCP3008 pins 0, 1 and 2, and you can see the raw value they generate in that table. I have no sensor on pins 3-7, the numbers you see there are likely electrical noise.

I suggest you do this part first, to see if you’re getting values for your sensors on the pins where they are connected. If you aren’t, it could be a wiring issue with the MCP3008 or your sensors themselves.

Next, I installed the Cayenne Python MQTT Library with:

git clone https://github.com/myDevicesIoT/Cayenne-MQTT-Python
cd Cayenne-MQTT-Python
python setup.py install

Once that’s installed, there will be an example MQTT Client in an examples subfolder. The example client will create an MQTT device on your Cayenne Dashboard after you plug in your values for these placeholders:

MQTT_USERNAME  = "MQTT_USERNAME"
MQTT_PASSWORD  = "MQTT_PASSWORD"
MQTT_CLIENT_ID = "MQTT_CLIENT_ID"

You can get these values by going to Add New > Device/Widget > Bring Your Own Thing. The example client will also publish a handful of dummy sensor data to show how writing sensor values to the Cayenne dashboard works. Since you want actual values here, I just merged in the part of the Adafruit simpletest.py file that reads the 3 pins that my sensors are on, and publishes to widgets on channels 1, 2 and 3. Here is my completed file:

#!/usr/bin/env python
import cayenne.client
import time
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008
 
#  Hardware SPI configuration:
SPI_PORT   = 0
SPI_DEVICE = 0
mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
 
 
# Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
MQTT_USERNAME  = "MQTTUSERNAME"
MQTT_PASSWORD  = "MQTTPASS"
MQTT_CLIENT_ID = "CLIENTID"
 
 
# The callback for when a message is received from Cayenne.
def on_message(message):
    print("message received: " + str(message))
    # If there is an error processing the message return an error string, otherwise return nothing.
   
client = cayenne.client.CayenneMQTTClient()
client.on_message = on_message
client.begin(MQTT_USERNAME, MQTT_PASSWORD, MQTT_CLIENT_ID)
 
i=0
timestamp = 0
 
while True:
    client.loop()
   
    if (time.time() > timestamp + 10):
        client.virtualWrite(1, mcp.read_adc(0))
        client.virtualWrite(2, mcp.read_adc(1))
        client.virtualWrite(3, mcp.read_adc(2))
        timestamp = time.time()
        i = i+1

Presuming you were able to read values using simpletest.py, this should work for you too. Just remember that you need to plug in your own MQTT Username/Password/Client ID, and if your sensors aren’t on pins 0, 1 and 2 (or if you have more or less than 3 sensors) you’ll need to edit the client.virtualWrite() statements to match what MCP3008 channels you want read.

One last thing: those values are just the raw readings coming off the ADC pins for each of my photoresistors, and sent to the Cayenne dashboard as generic numeric values with no data type. If I wanted them to be converted to lux (or in your case, volts), I’d have to write a bit of additional code to convert each reading to those units. If you consult the documentation for your sensors, you should be able to find how they encode their data and make this conversion. I can help you with code to do that, but I would need the specifics of what reading from the ADC = however many volts.

Phew :sweat:. I hope all of this makes sense, tried to make it easy for a non-coder. If you have any questions let me know!


#20

Well we are operating on 3 voltage sensors at the same channels…we were able to mount mcp3008 on cayenne… but the readings we are receiving are a little messed up…a hall effect voltage sensor for ac supply provides us with a sinusoidal waveform corresponding to the current with 2.5 volts as 0A and 5 volts as 10A. We just want the peak values of the output. Thanks anyways. But is there a way i can show modified outputs on cayenne itself.?