Need help with multiple arduino voltege sensor


#1

Hi, need help to setup voltage monitor. I use 4 voltage sensor conected to analog pin 2 to pin 5 and measured 4 diferent voltage., and show up on cayenne dashboard. But the problem is the witdget seem show the same pin. Can u please help me…

> Blockquote
// 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 analogInput2 2
#define analogInput3 3
#define analogInput4 4
#define analogInput5 5

// Volmeter
int analogInput = A2 - A5;
float vout = 0.0;
float vin = 0.0;
float R1 = 30000.0; //
float R2 = 7500.0; //
int value = 0;

// 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);
  Serial.print("DC VOLTMETER");
  pinMode(analogInput2, INPUT);
  pinMode(analogInput3, INPUT);
  pinMode(analogInput4, INPUT);
  pinMode(analogInput5, INPUT);
  Cayenne.begin(username, password, clientID);
}

void loop() {
  Cayenne.loop();



  //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(2, vin , "voltage", "v");
    Cayenne.virtualWrite(3, vin , "voltage", "v");
    Cayenne.virtualWrite(4, vin , "voltage", "v");
    Cayenne.virtualWrite(5, vin , "voltage", "v");
    //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);
    // read the value at analog input

    //Voltmeter

    value = analogRead(analogInput);
    vout = (value * 5.0) / 1024.0; // see text
    vin = vout / (R2 / (R1 + R2));
    if (vin < 0.50)
    {
      vin = 0.0;
    }
    

  }
}


#2

you should had continued in same topic in case of creating a new.

because you are reading from only one analog pin and

publish this value to all the 4 channels.


#3

I used this new code, seem to be wrong. Its still show the same measured.

// 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 analogInput0 0
#define analogInput1 1
#define analogInput2 2
#define analogInput3 3
#define analogInput4 4
#define analogInput5 5

// Volmeter
int analogInput = A2 - A5;
float vout = 0.0;
float vin = 0.0;
float R1 = 30000.0; //
float R2 = 7500.0; //
int value = 0;

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

unsigned long lastMillis = 0;

void setup() {
Serial.begin(9600);
pinMode(analogInput0, INPUT);
pinMode(analogInput1, INPUT);
pinMode(analogInput2, INPUT);
pinMode(analogInput3, INPUT);
pinMode(analogInput4, INPUT);
pinMode(analogInput5, INPUT);
Cayenne.begin(username, password, clientID);
}

void loop() {
Cayenne.loop();

//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(2, vin , “voltage”, “v”);
Cayenne.virtualWrite(3, vin , “voltage”, “v”);
Cayenne.virtualWrite(4, vin , “voltage”, “v”);
Cayenne.virtualWrite(5, vin , “voltage”, “v”);
//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);
// read the value at analog input

//Voltmeter
value = analogRead(analogInput);
vout = (value * 5.0) / 1024.0; // see text
vin = vout / (R2 / (R1 + R2));
if (vin < 0.05)
{
  vin = 0.0;
}

}

}

CAYENNE_OUT(2) // Get current value of analog pin 2
{
Cayenne.virtualWrite(analogInput2, vin);
}
CAYENNE_OUT(3) // Get current value of analog pin 3
{
Cayenne.virtualWrite(analogInput3, vin);
}
CAYENNE_OUT(4) // Get current value of analog pin 4
{
Cayenne.virtualWrite(analogInput4, vin);
}
CAYENNE_OUT(5) // Get current value of analog pin 5
{
Cayenne.virtualWrite(analogInput5, vin);
}


#4

Hi @shramiksalgaonkar, could you please help. Thanks


#5

in the above code of yours, are you reading data from all the analog pins or single pin?


#6

I wish to read from all analog pin, could you help me with the code?


#7

so add this for all your pins

value1 = analogRead(analogInput1);
vout1 = (value1 * 5.0) / 1024.0; // see text
vin1 = vout1/ (R2 / (R1 + R2));

and you might get error while reading multiple analog value so do dump and hold.

value1 = analogRead(analogInput1);
delay(10) ;
value1 = analogRead(analogInput1);
delay(10);
vout1 = (value1 * 5.0) / 1024.0; // see text
vin1 = vout1/ (R2 / (R1 + R2));

#8

Hi, sorry it’s take too long to reply. Finally, it’s work with multiple sensor. Thank u @shramiksalgaonkar.

This is the code :

> B// 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 analogInput0 0
#define analogInput1 1
#define analogInput2 2
#define analogInput3 3
#define analogInput4 4
#define analogInput5 5

// Volmeter
float vout2 = 0.0;
float vin2 = 0.0;
float R12 = 30000.0; //
float R22 = 7500.0; //
int value2 = 0;

// Volmeter
float vout3 = 0.0;
float vin3 = 0.0;
float R13 = 30000.0; //
float R23 = 7500.0; //
int value3 = 0;

// Volmeter
float vout4 = 0.0;
float vin4 = 0.0;
float R14 = 30000.0; //
float R24 = 7500.0; //
int value4 = 0;

// Volmeter
float vout5 = 0.0;
float vin5 = 0.0;
float R15 = 30000.0; //
float R25 = 7500.0; //
int value5 = 0;

// 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(analogInput0, INPUT);
  pinMode(analogInput1, INPUT);
  pinMode(analogInput2, INPUT);
  pinMode(analogInput3, INPUT);
  pinMode(analogInput4, INPUT);
  pinMode(analogInput5, INPUT);
  Cayenne.begin(username, password, clientID);
}

void loop() {
  Cayenne.loop();

  //Publish data every 1 seconds (1000 milliseconds). Change this value to publish at a different interval.
  if (millis() - lastMillis > 100) {
    lastMillis = millis();
    //Write data to Cayenne here. This example just sends the current uptime in milliseconds.
    Cayenne.virtualWrite(2, vin2 , "voltage", "v");
    Cayenne.virtualWrite(3, vin3 , "voltage", "v");
    Cayenne.virtualWrite(4, vin4 , "voltage", "v");
    Cayenne.virtualWrite(5, vin5 , "voltage", "v");
    //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);
    // read the value at analog input

    //Voltmeter Battery
    value2 = analogRead(analogInput2);
    delay(1000) ;
    vout2 = (value2 * 5.0) / 1024.0; // see text
    vin2 = vout2 / (R22 / (R12 + R22));
    if (vin2 < 1.05)
    {
      vin2 = 0.0;
    }

    //Voltmeter Charger
    value3 = analogRead(analogInput3);
    delay(1000) ;
    vout3 = (value3 * 5.0) / 1024.0; // see text
    vin3 = vout3 / (R23 / (R13 + R23));
    if (vin3 < 1.05)
    {
      vin3 = 0.0;
    }
    //Voltmeter 12 V Output
    value4 = analogRead(analogInput4);
    delay(1000) ;
    vout4 = (value4 * 5.0) / 1024.0; // see text
    vin4 = vout4 / (R24 / (R14 + R24));
    if (vin4 < 1.05)
    {
      vin4 = 0.0;
    }
    //Voltmeter 24 v Output
    value5 = analogRead(analogInput5);
    delay(1000) ;
    vout5 = (value5 * 5.0) / 1024.0; // see text
    vin5 = vout5 / (R25 / (R15 + R25));
    if (vin5 < 1.05)
    {
      vin5 = 0.0;
    }


  }

}


CAYENNE_OUT(2) // Get current value of analog pin 2
{
  Cayenne.virtualWrite(analogInput2, vin2);
}
CAYENNE_OUT(3) // Get current value of analog pin 3
{
  Cayenne.virtualWrite(analogInput3, vin3);
}
CAYENNE_OUT(4) // Get current value of analog pin 4
{
  Cayenne.virtualWrite(analogInput4, vin4);
}
CAYENNE_OUT(5) // Get current value of analog pin 5
{
  Cayenne.virtualWrite(analogInput5, vin5);
}lockquote

#9

great to hear that it is working. but why you sending values two times to the the channel widget?


#10

Hi @shramiksalgaonkar, which part you are referring to?


#11

is same as


#12

Well, thanks for the correction, i will revise it again.


#13

use the first one and delete CAYENNE_OUT().


#14

Roger that.