Voltage Sensors wrong and no Onewire



  • Hi,

    I need more eyes and help with code on this.
    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).
    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!).

    Measured at battery and at voltage divider:
    4,01 - 0,353
    8,02 - 0,715
    12,03 - 1,064
    16,05 - 1,419
    20,05 - 1,775
    24,06V - 2,15 V

    My 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 <MySensors.h>
    #include <OneWire.h>
    #include <DallasTemperature.h>
    
    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(&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<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<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<numSensors; i++) {   
    
         present(i, S_TEMP);
      }
    }
    
    void loop()     
    {    
    // TEMPERATURES   
      sensors.requestTemperatures();
      for (int i=0; i<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<6; k++) {
        while (sampleCount < 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 &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());
      }
    }
    

    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?

    1. why do the voltages read wrong? Why do the values come out negative (see the "-" in the voltMilli calculation)?
    2. why do the ds18b20's not show up?
    3. how do I get the values from the for-loop to play with? The "float voltage_array[k] = (voltMilli / 1000)" causes errors

Log in to reply
 

Suggested Topics

  • 11
  • 12
  • 5
  • 5
  • 5
  • 13
  • 1
  • 6

257
Online

9.6k
Users

10.2k
Topics

106.1k
Posts