Current Sensor


#1

Hi everyone, how to add current sensor (Ampere sensor) ACS712?FREELAB-ACS712-5A-Sensor-Arus-AC-dan-DC-Hall-Effect-Merchant--SKU08516355-2016826145812


#2

we need more details like

  1. which device you are using.
  2. where you able to get reading from the sensor without the cayenne code. http://henrysbench.capnfatz.com/henrys-bench/arduino-current-measurements/the-acs712-current-sensor-with-an-arduino/
  3. was your device connected to cayenne.Adding a New Device using MQTT

then using cayenne for your project is very easy.


#3

Im using Arduino uno with W5100, and I’d like to measure the DC current with cayenne using that sensor, can u please help me with code? Thanks


#4

i can help you but you need to start with doing something. first, see if you can add the sensor to the Arduino and get values in the serial monitor. then add a device to cayenne. both links are provided in the previous reply.


#5
#include <ACS712.h>

#include <NTPClient.h>
/*
Measuring Current Using ACS712
*/
const int analogIn = A0;
int mVperAmp = 66; // use 100 for 20A Module and 66 for 30A Module
int RawValue= 0;
int ACSoffset = 2500; 
double Voltage = 0;
double Amps = 0;
// This example shows how to connect to Cayenne using an Ethernet W5100 shield and send/receive sample data.

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTEthernet.h>

#define VIRTUAL_CHANNEL_2 2
#define VIRTUAL_CHANNEL_3 3
#define VIRTUAL_CHANNEL_4 4
#define VIRTUAL_CHANNEL_5 5
#define VIRTUAL_CHANNEL_6 6
#define VIRTUAL_CHANNEL_7 7
#define VIRTUAL_CHANNEL_8 8
#define SENSOR_PIN 0
#define VIRTUAL_CHANNEL 30

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";


unsigned long lastMillis = 0;

void setup() {
  Serial.begin(9600);
  pinMode(VIRTUAL_CHANNEL_2, OUTPUT);
  pinMode(VIRTUAL_CHANNEL_3, OUTPUT);
  pinMode(VIRTUAL_CHANNEL_4, OUTPUT);
  pinMode(VIRTUAL_CHANNEL_5, OUTPUT);
  pinMode(VIRTUAL_CHANNEL_6, OUTPUT);
  pinMode(VIRTUAL_CHANNEL_7, OUTPUT);
  pinMode(VIRTUAL_CHANNEL_8, OUTPUT);
  Cayenne.begin(username, password, clientID);
}

void loop() {
  Cayenne.loop();
}

// This function is called at intervals to send sensor data to Cayenne.
CAYENNE_OUT(VIRTUAL_CHANNEL)
{
  //Publish data every 1 seconds (1000 milliseconds). Change this value to publish at a different interval.
  if (millis() - lastMillis > 1000) {
    lastMillis = millis();
    //Write data to Cayenne here. This example just sends the current uptime in milliseconds.
    Cayenne.virtualWrite(0, lastMillis);
    Cayenne.virtualWrite(VIRTUAL_CHANNEL, analogRead(SENSOR_PIN));
    //Some examples of other functions you can use to send data.
    //Cayenne.celsiusWrite(1, 22.0);
    //Cayenne.luxWrite(2, 700);
    //Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
  }
  
}

CAYENNE_IN(1) // Get current value of led 1 button
{
  int x = getValue.asInt();
  digitalWrite(VIRTUAL_CHANNEL_2, x);
}
CAYENNE_IN(2) // Get current value of led 2 button
{
  int x1 = getValue.asInt();
  digitalWrite(VIRTUAL_CHANNEL_3, x1);
  
  }
  CAYENNE_IN(3) // Get current value of led 3 button
{
  int x2 = getValue.asInt();
  digitalWrite(VIRTUAL_CHANNEL_4, x2);
}
CAYENNE_IN(4) // Get current value of led 4 button
{
  int x3 = getValue.asInt();
  digitalWrite(VIRTUAL_CHANNEL_5, x3);
  
  }
  CAYENNE_IN(5) // Get current value of led 5 button
{
  int x4 = getValue.asInt();
  digitalWrite(VIRTUAL_CHANNEL_6, x4);
  
  }
  CAYENNE_IN(6) // Get current value of led 6 button
{
  int x5 = getValue.asInt();
  digitalWrite(VIRTUAL_CHANNEL_7, x5);


RawValue = analogRead(analogIn);
 Voltage = (RawValue / 1024.0) * 5000; // Gets you mV
 Amps = ((Voltage - ACSoffset) / mVperAmp);
 
 
 Serial.print("Raw Value = " ); // shows pre-scaled value 
 Serial.print(RawValue); 
 Serial.print("\t mV = "); // shows the voltage measured 
 Serial.print(Voltage,3); // the '3' after voltage allows you to display 3 digits after decimal point
 Serial.print("\t Amps = "); // shows the voltage measured 
 Serial.println(Amps,3); // the '3' after voltage allows you to display 3 digits after decimal point
 delay(2500); 


}

I still can’t measure the curent :disappointed_relieved::disappointed_relieved:


#6

follow this tutorial and see if you get current reading, http://henrysbench.capnfatz.com/henrys-bench/arduino-current-measurements/the-acs712-current-sensor-with-an-arduino/