<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Voltage Sensors wrong and no Onewire]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">I need more eyes and help with code on this.<br />
I have a solar battery (6S Li-ion, Nissan Leaf cells) and I want to monitor voltages, temps and (later) control a safety relay/contactor. The Arduino (3.3v pro mini is powered by a adjustable step down converter set to 3.65V -- just couldnt get 3.3V no matter how closely I turned the knob).<br />
The voltages come out wrong (1M+100kOhm dividers) - even with a multiplier to even it out. The DS18B20's dont register at all (with simple example sketches they read fine!).</p>
<p dir="auto">Measured at battery and at voltage divider:<br />
4,01 - 0,353<br />
8,02 - 0,715<br />
12,03 - 1,064<br />
16,05 - 1,419<br />
20,05 - 1,775<br />
24,06V - 2,15 V</p>
<p dir="auto">My code</p>
<pre><code>#define MY_DEBUG                             // Enable debug prints to serial monitor
#define MY_RADIO_RF24 // Enable and select radio type attached
#define MY_NODE_ID 22

#define voltagePin1 19 //A5 -- 1S
#define voltagePin2 18
#define voltagePin3 17
#define voltagePin4 16
#define voltagePin5 15
#define voltagePin6 14 //A0 -- 6S
#define RELAY1 5
#define RELAY2 6
#define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected 
#define MAX_ATTACHED_DS18B20 2

#include &lt;MySensors.h&gt;
#include &lt;OneWire.h&gt;
#include &lt;DallasTemperature.h&gt;

int voltSenseMax = 40150; // * Voltage divider R1 1 megaohm  R2 100 kilohms  -- 3.65V VCC
int sampleCount = 0;  
int sum = 0;              // sum of samples taken 
int numSamples = 5;  
int voltMilli;
float voltage_array[6] = { 0, 0, 0, 0, 0, 0,};
OneWire oneWire(ONE_WIRE_BUS); // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
DallasTemperature sensors(&amp;oneWire); // Pass the oneWire reference to Dallas Temperature. 
float lastTemperature[MAX_ATTACHED_DS18B20];
int numSensors=0;
DeviceAddress tempDeviceAddress;
bool receivedConfig = false;
bool metric = true;

MyMessage msg_S_MULTIMETER(0, V_VOLTAGE);
MyMessage msg(0,V_TEMP);

void setup()
{
  sensors.begin(); delay(10);
  
for (int j=14; j&lt;20; j++) {
 pinMode(j, INPUT);
  }
  
 pinMode(RELAY1, OUTPUT);
 digitalWrite(RELAY1, HIGH);
 pinMode(RELAY2, OUTPUT);
 digitalWrite(RELAY2, LOW);

}

void presentation()  
{  
 sendSketchInfo("Solar Battery Monitor", "v05092020");    // Send the sketch version information to the gateway and Controller

  for (int j=14; j&lt;20; j++) {
  present(j, S_MULTIMETER);
  }
  
  present(RELAY1, S_BINARY, "Cutoff Relay1");
  present(RELAY2, S_BINARY, "Cutoff Relay2");
  
  numSensors = sensors.getDeviceCount();
  Serial.print("Locating devices...");
  Serial.print("Found ");
  Serial.print(numSensors, DEC);
  Serial.println(" devices.");
  for (int i=0; i&lt;numSensors; i++) {   

     present(i, S_TEMP);
  }
}

void loop()     
{    
// TEMPERATURES   
  sensors.requestTemperatures();
  for (int i=0; i&lt;numSensors; i++) {
    if(sensors.getAddress(tempDeviceAddress, i)){
    Serial.print("Temperature for device: "); Serial.println(i,DEC);
    float tempC = sensors.getTempC(tempDeviceAddress);
    Serial.print("Temp C: ");
    Serial.println(tempC);
    send(msg.setSensor(i).set(tempC,1));
    }
  }

// VOLTAGES
  for (int k=0; k&lt;6; k++) {
    while (sampleCount &lt; numSamples) {
        sum += analogRead(k+14);
        sampleCount++;
        delay(5);
        }
   
  int voltMilli = -(map((sum / numSamples),0,1023,0,voltSenseMax))*1.5988;  // map the reading and get the result in millivolts
  send(msg_S_MULTIMETER.setSensor(k+14).set((voltMilli / 1000.0), 2));   // Divide by 1000 to convert back to volts to two decimal places, send data to controller.                                                                                                                                            // send voltage message to gateway with 1 decimal place
  Serial.print(voltMilli / 1000.0);
  Serial.println(" V");
//  float voltage_array[k] = (voltMilli / 1000);
  sampleCount = 0;
  sum = 0;
  delay(5000);
  }

delay(15000);

} 

void receive(const MyMessage &amp;message)
{
  if (message.type==V_STATUS) {
    digitalWrite(message.sensor, message.getBool());
    saveState(message.sensor, message.getBool());
    Serial.print("Incoming change for sensor:"); Serial.print(message.sensor);
    Serial.print(", New status: "); Serial.println(message.getBool());
  }
}
</code></pre>
<p dir="auto">I would also like to get the voltages as values to play with in the code (to monitor high and low voltages) but I cant make "voltage_array" work. Can someone with more arduino-code powers help here?</p>
<ol>
<li>why do the voltages read wrong? Why do the values come out negative (see the "-" in the voltMilli calculation)?</li>
<li>why do the ds18b20's not show up?</li>
<li>how do I get the values from the for-loop to play with? The "float voltage_array[k] = (voltMilli / 1000)" causes errors</li>
</ol>
]]></description><link>https://forum.mysensors.org/topic/11362/voltage-sensors-wrong-and-no-onewire</link><generator>RSS for Node</generator><lastBuildDate>Wed, 22 Jul 2026 04:03:52 GMT</lastBuildDate><atom:link href="https://forum.mysensors.org/topic/11362.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 11 Sep 2020 21:52:39 GMT</pubDate><ttl>60</ttl></channel></rss>