Optical pulse sensor



  • Hello all,

    I have a the following problem with my sensor. As soon as the last value is read from Domoticz (V_VAR1), it starts sending pulses to Domoticz without even the optical sensor being connected to Arduino.

    Code:

    /**
     * The MySensors Arduino library handles the wireless radio link and protocol
     * between your home built sensors/actuators and HA controller of choice.
     * The sensors forms a self healing radio network with optional repeaters. Each
     * repeater and gateway builds a routing tables in EEPROM which keeps track of the
     * network topology allowing messages to be routed to nodes.
     *
     * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
     * Copyright (C) 2013-2015 Sensnology AB
     * Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
     *
     * Documentation: http://www.mysensors.org
     * Support Forum: http://forum.mysensors.org
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * version 2 as published by the Free Software Foundation.
     *
     *******************************
     *
     * REVISION HISTORY
     * Version 1.0 - Henrik EKblad
     * 
     * DESCRIPTION
     * This sketch provides an example how to implement a distance sensor using HC-SR04 
     * Use this sensor to measure KWH and Watt of your house meeter
     * You need to set the correct pulsefactor of your meeter (blinks per KWH).
     * The sensor starts by fetching current KWH value from gateway.
     * Reports both KWH and Watt back to gateway.
     *
     * Unfortunately millis() won't increment when the Arduino is in 
     * sleepmode. So we cannot make this sensor sleep if we also want 
     * to calculate/report watt-number.
     * http://www.mysensors.org/build/pulse_power
     */
    
    #include <SPI.h>
    #include <MySensor.h>  
    
    #define DIGITAL_INPUT_SENSOR 3  // The digital input you attached your light sensor.  (Only 2 and 3 generates interrupt!)
    #define SENSOR_INTERRUPT DIGITAL_INPUT_SENSOR-3 // Usually the interrupt = pin -2 (on uno/nano anyway)
    
    #define PULSE_FACTOR 1000       // Nummber of blinks per KWH of your meeter
    #define SLEEP_MODE false        // Watt-value can only be reported when sleep mode is false.
    #define MAX_WATT 10000          // Max watt value to report. This filetrs outliers.
    #define CHILD_ID 4              // Id of the sensor child
    unsigned long SEND_FREQUENCY = 20000; // Minimum time between send (in milliseconds). We don't wnat to spam the gateway.
    MySensor gw;
    double ppwh = ((double)PULSE_FACTOR)/1000; // Pulses per watt hour
    boolean pcReceived = false;
    volatile unsigned long pulseCount = 0;   
    volatile unsigned long lastBlink = 0;
    volatile unsigned long watt = 0;
    unsigned long oldPulseCount = 0;   
    unsigned long oldWatt = 0;
    double oldKwh;
    float interval;
    unsigned long lastSend;
    MyMessage wattMsg(CHILD_ID,V_WATT);
    MyMessage kwhMsg(CHILD_ID,V_KWH);
    MyMessage pcMsg(CHILD_ID,V_VAR1);
    
    
    void setup()  
    {  
      gw.begin(incomingMessage);
    
      pinMode(DIGITAL_INPUT_SENSOR, INPUT_PULLUP);
      
      // Send the sketch version information to the gateway and Controller
      gw.sendSketchInfo("Energy Meter", "1.1");
    
      // Register this device as power sensor
      gw.present(CHILD_ID, S_POWER);
    
      // Fetch last known pulse count value from gw
      gw.request(CHILD_ID, V_VAR1);
    
      lastSend=millis();
      
      attachInterrupt(SENSOR_INTERRUPT, onPulse, RISING);
      
    }
    
    
    void loop()     
    { 
      gw.process();
      unsigned long now = millis();
      // Only send values at a maximum frequency or woken up from sleep
      bool sendTime = now - lastSend > SEND_FREQUENCY;
      if (pcReceived && (SLEEP_MODE || sendTime)) {
        // New watt value has been calculated  
        if (!SLEEP_MODE && watt != oldWatt) {
          // Check that we dont get unresonable large watt value. 
          // could hapen when long wraps or false interrupt triggered
          if (watt<((unsigned long)MAX_WATT)) {
            gw.send(wattMsg.set(watt));  // Send watt value to gw 
          }  
          Serial.print("Watt:");
          Serial.println(watt);
          oldWatt = watt;
        }
      
        // Pulse cout has changed
        if (pulseCount != oldPulseCount) {
          gw.send(pcMsg.set(pulseCount));  // Send pulse count value to gw 
          double kwh = ((double)pulseCount/((double)PULSE_FACTOR));     
          oldPulseCount = pulseCount;
          if (kwh != oldKwh) {
            gw.send(kwhMsg.set(kwh, 4));  // Send kwh value to gw 
            oldKwh = kwh;
          }
        }    
        lastSend = now;
      } else if (sendTime && !pcReceived) {
        // No count received. Try requesting it again
        gw.request(CHILD_ID, V_VAR1);
        lastSend=now;
      }
      
      if (SLEEP_MODE) {
        gw.sleep(SEND_FREQUENCY);
      }
    }
    
    void incomingMessage(const MyMessage &message) {
      if (message.type==V_VAR1) {  
        pulseCount = oldPulseCount = message.getLong();
        Serial.print("Received last pulse count from gw:");
        Serial.println(pulseCount);
        pcReceived = true;
      }
    }
    
    void onPulse()     
    { 
      if (!SLEEP_MODE) {
        unsigned long newBlink = micros();  
        unsigned long interval = newBlink-lastBlink;
        if (interval<10000L) { // Sometimes we get interrupt on RISING
          return;
        }
        watt = (3600000000.0 /interval) / ppwh;
        lastBlink = newBlink;
      } 
      pulseCount++;
      Serial.print(" | Pulse Duration: ");
      Serial.print(interval, 2);
      Serial.println("");
      Serial.print(" | Pulse Count: ");
      Serial.print(pulseCount);
    
    }
    
    
    void output() {
      /*
        Output Results - The result of its calculations are to
        be output to the screen (via Serial port).
      */
      Serial.println("");
      Serial.print(" | Power (W): ");
      Serial.print((float)watt, 2);
      Serial.print(" | Energy (kWh): ");
      Serial.print(oldKwh, 2);
     
    }
    

    Terminal output:

    Code:

    send: 10-10-0-0 s=255,c=0,t=17,pt=0,l=3,sg=0,st=ok:1.5
    send: 10-10-0-0 s=255,c=3,t=6,pt=1,l=1,sg=0,st=ok:0
    read: 0-0-10 s=255,c=3,t=6,pt=0,l=1,sg=0:M
    sensor started, id=10, parent=0, distance=1
    send: 10-10-0-0 s=255,c=3,t=11,pt=0,l=12,sg=0,st=ok:Energy Meter
    send: 10-10-0-0 s=255,c=3,t=12,pt=0,l=3,sg=0,st=ok:1.1
    send: 10-10-0-0 s=4,c=0,t=13,pt=0,l=0,sg=0,st=ok:
    send: 10-10-0-0 s=4,c=2,t=24,pt=0,l=0,sg=0,st=ok:
    | Pulse Duration: 0.00
    | Pulse Count: 1read: 0-0-10 s=4,c=2,t=24,pt=0,l=5,sg=0:34940
    Received last pulse count from gw:34940
    | Pulse Duration: 0.00
    | Pulse Count: 34941 | Pulse Duration: 0.00
    | Pulse Count: 34942 | Pulse Duration: 0.00
    | Pulse Count: 34943 | Pulse Duration: 0.00
    | Pulse Count: 34944 | Pulse Duration: 0.00
    | Pulse Count: 34945 | Pulse Duration: 0.00
    | Pulse Count: 34946 | Pulse Duration: 0.00
    | Pulse Count: 34947 | Pulse Duration: 0.00
    | Pulse Count: 34948 | Pulse Duration: 0.00
    | Pulse Count: 34949 | Pulse Duration: 0.00
    | Pulse Count: 34950 | Pulse Duration: 0.00
    | Pulse Count: 34951 | Pulse Duration: 0.00
    | Pulse Count: 34952 | Pulse Duration: 0.00
    | Pulse Count: 34953 | Pulse Duration: 0.00
    | Pulse Count: 34954 | Pulse Duration: 0.00
    | Pulse Count: 34955 | Pulse Duration: 0.00
    | Pulse Count: 34956 | Pulse Duration: 0.00
    | Pulse Count: 34957 | Pulse Duration: 0.00
    | Pulse Count: 34958 | Pulse Duration: 0.00
    | Pulse Count: 34959 | Pulse Duration: 0.00
    | Pulse Count: 34960 | Pulse Duration: 0.00
    | Pulse Count: 34961 | Pulse Duration: 0.00
    | Pulse Count: 34962 | Pulse Duration: 0.00
    | Pulse Count: 34963 | Pulse Duration: 0.00
    | Pulse Count: 34964 | Pulse Duration: 0.00
    | Pulse Count: 34965 | Pulse Duration: 0.00
    | Pulse Count: 34966 | Pulse Duration: 0.00
    | Pulse Count: 34967 | Pulse Duration: 0.00
    | Pulse Count: 34968 | Pulse Duration: 0.00
    | Pulse Count: 34969 | Pulse Duration: 0.00
    | Pulse Count: 34970 | Pulse Duration: 0.00
    | Pulse Count: 34971 | Pulse Duration: 0.00
    | Pulse Count: 34972 | Pulse Duration: 0.00
    | Pulse Count: 34973 | Pulse Duration: 0.00
    | Pulse Count: 34974 | Pulse Duration: 0.00
    | Pulse Count: 34975 | Pulse Duration: 0.00
    | Pulse Count: 34976 | Pulse Duration: 0.00
    | Pulse Count: 34977 | Pulse Duration: 0.00
    | Pulse Count: 34978 | Pulse Duration: 0.00
    | Pulse Count: 34979 | Pulse Duration: 0.00
    | Pulse Count: 34980 | Pulse Duration: 0.00
    | Pulse Count: 34981 | Pulse Duration: 0.00
    | Pulse Count: 34982 | Pulse Duration: 0.00
    | Pulse Count: 34983 | Pulse Duration: 0.00
    | Pulse Count: 34984 | Pulse Duration: 0.00
    | Pulse Count: 34985 | Pulse Duration: 0.00
    | Pulse Count: 34986 | Pulse Duration: 0.00
    | Pulse Count: 34987 | Pulse Duration: 0.00
    | Pulse Count: 34988 | Pulse Duration: 0.00
    | Pulse Count: 34989 | Pulse Duration: 0.00
    | Pulse Count: 34990 | Pulse Duration: 0.00
    | Pulse Count: 34991 | Pulse Duration: 0.00
    | Pulse Count: 34992 | Pulse Duration: 0.00
    | Pulse Count: 34993 | Pulse Duration: 0.00
    | Pulse Count: 34994 | Pulse Duration: 0.00
    | Pulse Count: 34995 | Pulse Duration: 0.00
    | Pulse Count: 34996 | Pulse Duration: 0.00
    | Pulse Count: 34997 | Pulse Duration: 0.00
    | Pulse Count: 34998 | Pulse Duration: 0.00
    | Pulse Count: 34999 | Pulse Duration: 0.00
    | Pulse Count: 35000 | Pulse Duration: 0.00
    | Pulse Count: 35001 | Pulse Duration: 0.00
    | Pulse Count: 35002 | Pulse Duration: 0.00
    | Pulse Count: 35003 | Pulse Duration: 0.00
    | Pulse Count: 35004 | Pulse Duration: 0.00
    | Pulse Count: 35005 | Pulse Duration: 0.00
    | Pulse Count: 35006 | Pulse Duration: 0.00
    | Pulse Count: 35007 | Pulse Duration: 0.00
    | Pulse Count: 35008 | Pulse Duration: 0.00
    | Pulse Count: 35009 | Pulse Duration: 0.00
    | Pulse Count: 35010 | Pulse Duration: 0.00
    | Pulse Count: 35011 | Pulse Duration: 0.00
    | Pulse Count: 35012 | Pulse Duration: 0.00
    | Pulse Count: 35013 | Pulse Duration: 0.00
    | Pulse Count: 35014 | Pulse Duration: 0.00
    | Pulse Count: 35015 | Pulse Duration: 0.00
    | Pulse Count: 35016 | Pulse Duration: 0.00
    | Pulse Count: 35017 | Pulse Duration: 0.00
    | Pulse Count: 35018 | Pulse Duration: 0.00
    | Pulse Count: 35019 | Pulse Duration: 0.00
    | Pulse Count: 35020 | Pulse Duration: 0.00
    | Pulse Count: 35021 | Pulse Duration: 0.00
    | Pulse Count: 35022 | Pulse Duration: 0.00
    | Pulse Count: 35023 | Pulse Duration: 0.00
    | Pulse Count: 35024 | Pulse Duration: 0.00
    | Pulse Count: 35025 | Pulse Duration: 0.00
    | Pulse Count: 35026 | Pulse Duration: 0.00
    | Pulse Count: 35027 | Pulse Duration: 0.00
    | Pulse Count: 35028 | Pulse Duration: 0.00
    | Pulse Count: 35029 | Pulse Duration: 0.00
    | Pulse Count: 35030 | Pulse Duration: 0.00
    | Pulse Count: 35031 | Pulse Duration: 0.00
    | Pulse Count: 35032 | Pulse Duration: 0.00
    | Pulse Count: 35033 | Pulse Duration: 0.00
    | Pulse Count: 35034 | Pulse Duration: 0.00
    | Pulse Count: 35035 | Pulse Duration: 0.00
    | Pulse Count: 35036 | Pulse Duration: 0.00
    | Pulse Count: 35037 | Pulse Duration: 0.00
    | Pulse Count: 35038 | Pulse Duration: 0.00
    | Pulse Count: 35039 | Pulse Duration: 0.00
    | Pulse Count: 35040 | Pulse Duration: 0.00
    | Pulse Count: 35041 | Pulse Duration: 0.00
    | Pulse Count: 35042 | Pulse Duration: 0.00
    | Pulse Count: 35043 | Pulse Duration: 0.00
    | Pulse Count: 35044 | Pulse Duration: 0.00
    | Pulse Count: 35045 | Pulse Duration: 0.00
    | Pulse Count: 35046 | Pulse Duration: 0.00
    | Pulse Count: 35047 | Pulse Duration: 0.00
    | Pulse Count: 35048 | Pulse Duration: 0.00
    | Pulse Count: 35049 | Pulse Duration: 0.00
    | Pulse Count: 35050 | Pulse Duration: 0.00
    | Pulse Count: 35051 | Pulse Duration: 0.00
    | Pulse Count: 35052 | Pulse Duration: 0.00
    | Pulse Count: 35053 | Pulse Duration: 0.00
    | Pulse Count: 35054 | Pulse Duration: 0.00
    | Pulse Count: 35055 | Pulse Duration: 0.00
    | Pulse Count: 35056 | Pulse Duration: 0.00
    | Pulse Count: 35057 | Pulse Duration: 0.00
    | Pulse Count: 35058 | Pulse Duration: 0.00
    | Pulse Count: 35059 | Pulse Duration: 0.00
    | Pulse Count: 35060 | Pulse Duration: 0.00
    | Pulse Count: 35061 | Pulse Duration: 0.00
    | Pulse Count: 35062 | Pulse Duration: 0.00
    | Pulse Count: 35063 | Pulse Duration: 0.00
    | Pulse Count: 35064 | Pulse Duration: 0.00
    | Pulse Count: 35065 | Pulse Duration: 0.00
    | Pulse Count: 35066 | Pulse Duration: 0.00
    | Pulse Count: 35067 | Pulse Duration: 0.00
    | Pulse Count: 35068 | Pulse Duration: 0.00
    | Pulse Count: 35069 | Pulse Duration: 0.00
    | Pulse Count: 35070 | Pulse Duration: 0.00
    | Pulse Count: 35071 | Pulse Duration: 0.00
    | Pulse Count: 35072 | Pulse Duration: 0.00
    | Pulse Count: 35073 | Pulse Duration: 0.00
    | Pulse Count: 35074 | Pulse Duration: 0.00
    | Pulse Count: 35075 | Pulse Duration: 0.00
    | Pulse Count: 35076 | Pulse Duration: 0.00
    | Pulse Count: 35077 | Pulse Duration: 0.00
    | Pulse Count: 35078 | Pulse Duration: 0.00
    | Pulse Count: 35079 | Pulse Duration: 0.00
    | Pulse Count: 35080 | Pulse Duration: 0.00
    | Pulse Count: 35081 | Pulse Duration: 0.00
    | Pulse Count: 35082 | Pulse Duration: 0.00
    | Pulse Count: 35083 | Pulse Duration: 0.00
    | Pulse Count: 35084 | Pulse Duration: 0.00
    | Pulse Count: 35085 | Pulse Duration: 0.00
    | Pulse Count: 35086 | Pulse Duration: 0.00
    | Pulse Count: 35087 | Pulse Duration: 0.00
    | Pulse Count: 35088 | Pulse Duration: 0.00
    | Pulse Count: 35089 | Pulse Duration: 0.00
    | Pulse Count: 35090 | Pulse Duration: 0.00
    | Pulse Count: 35091 | Pulse Duration: 0.00
    | Pulse Count: 35092 | Pulse Duration: 0.00
    | Pulse Count: 35093 | Pulse Duration: 0.00
    | Pulse Count: 35094 | Pulse Duration: 0.00
    | Pulse Count: 35095 | Pulse Duration: 0.00
    | Pulse Count: 35096 | Pulse Duration: 0.00
    | Pulse Count: 35097 | Pulse Duration: 0.00
    | Pulse Count: 35098 | Pulse Duration: 0.00
    | Pulse Count: 35099 | Pulse Duration: 0.00
    | Pulse Count: 35100 | Pulse Duration: 0.00
    | Pulse Count: 35101 | Pulse Duration: 0.00
    | Pulse Count: 35102 | Pulse Duration: 0.00
    | Pulse Count: 35103 | Pulse Duration: 0.00
    | Pulse Count: 35104 | Pulse Duration: 0.00
    | Pulse Count: 35105 | Pulse Duration: 0.00
    | Pulse Count: 35106 | Pulse Duration: 0.00
    | Pulse Count: 35107 | Pulse Duration: 0.00
    | Pulse Count: 35108 | Pulse Duration: 0.00
    | Pulse Count: 35109 | Pulse Duration: 0.00
    | Pulse Count: 35110 | Pulse Duration: 0.00
    | Pulse Count: 35111 | Pulse Duration: 0.00
    | Pulse Count: 35112 | Pulse Duration: 0.00
    | Pulse Count: 35113 | Pulse Duration: 0.00
    | Pulse Count: 35114 | Pulse Duration: 0.00
    | Pulse Count: 35115 | Pulse Duration: 0.00
    | Pulse Count: 35116 | Pulse Duration: 0.00
    | Pulse Count: 35117 | Pulse Duration: 0.00
    | Pulse Count: 35118 | Pulse Duration: 0.00
    | Pulse Count: 35119 | Pulse Duration: 0.00
    | Pulse Count: 35120 | Pulse Duration: 0.00
    | Pulse Count: 35121 | Pulse Duration: 0.00
    | Pulse Count: 35122 | Pulse Duration: 0.00
    | Pulse Count: 35123 | Pulse Duration: 0.00
    | Pulse Count: 35124 | Pulse Duration: 0.00
    | Pulse Count: 35125 | Pulse Duration: 0.00
    | Pulse Count: 35126 | Pulse Duration: 0.00
    | Pulse Count: 35127 | Pulse Duration: 0.00
    | Pulse Count: 35128 | Pulse Duration: 0.00
    | Pulse Count: 35129 | Pulse Duration: 0.00
    | Pulse Count: 35130 | Pulse Duration: 0.00
    | Pulse Count: 35131 | Pulse Duration: 0.00
    | Pulse Count: 35132 | Pulse Duration: 0.00
    | Pulse Count: 35133 | Pulse Duration: 0.00
    | Pulse Count: 35134 | Pulse Duration: 0.00
    | Pulse Count: 35135 | Pulse Duration: 0.00
    | Pulse Count: 35136 | Pulse Duration: 0.00
    | Pulse Count: 35137 | Pulse Duration: 0.00
    | Pulse Count: 35138 | Pulse Duration: 0.00
    | Pulse Count: 35139 | Pulse Duration: 0.00
    | Pulse Count: 35140 | Pulse Duration: 0.00
    | Pulse Count: 35141 | Pulse Duration: 0.00
    | Pulse Count: 35142 | Pulse Duration: 0.00
    | Pulse Count: 35
    

    I have an optical light sensor on the Arduino Nano D3.
    Gateway is receiving like 5000 pulses every minute. (Even without the optical sensor connected).

    Any ideas please?


  • Mod

    1. If the interrupt pin is left floating (=not connected to anything) you can get lots and lots of spurious interrupts. Connect the pin to GND or Vcc through a resistor (>50ohm) to get rid of that problem.
    2. Even when the optical sensor is connected, you might have a bouncing problem. Look up "debouncing" if you haven't already. There are lots of weird things happening when we leave the "safe" digital world 🙂

  • Hardware Contributor

    This post is deleted!


  • @mfalkvidd said:

    1. If the interrupt pin is left floating (=not connected to anything) you can get lots and lots of spurious interrupts. Connect the pin to GND or Vcc through a resistor (>50ohm) to get rid of that problem.
    2. Even when the optical sensor is connected, you might have a bouncing problem. Look up "debouncing" if you haven't already. There are lots of weird things happening when we leave the "safe" digital world 🙂

    It looks that noise is gone by connecting 10k resistor to VCC (pull-up?). Thanks for heads-up


  • Mod

    Yes, that's called a pull-up 😄 Great to hear it solved the problem!


Log in to reply
 

Suggested Topics

  • 5
  • 5
  • 5
  • 8
  • 1
  • 1

0
Online

11.4k
Users

11.1k
Topics

112.7k
Posts