Steven Jones
A blog of electronics and programming

Spot counting - JeeNode prototype

Right, so to get my gas meter monitoring project going, I'm going to start by writing a program to pop on a JeeNode that can count spots.

I think the program needs to:

  1. Be designed for low power use - it might not actually be low power to begin with, but it needs to be designed with this in mind
  2. Be able to read the spot 'state' from the phototransistor.
  3. Remember the last few states and check to see if a new state has appeared.
  4. Remember a 'count' of spots.
  5. Increase this count of spots when a new spot is read.
  6. Broadcast this count every minute
  7. Broadcast this count when it changes

Reading around, it seems that the way to keep this all low power is to plan to have the JeeNode sleeping most of the time. I'm going to periodically have something that broadcasts the current count, and something that checks to see the state of the phototransistor (and power it up in the process). The JeeNode will be asleep the rest of the time.

I will remember something like the last four 'states' read from the phototransistor, and check to see what they look like. When detecting a 'new' spot they should look like:

HLLL

and then become something like:

HHLL

and eventually

LLLH

so I can just check to see if the current readings match the new spot state, and if so increment an internal counter.

I'm not just going to broadcast the spot appearing, because if the radio transmission is lost then things will get out of sync, but this way, it doesn't matter, the counts will match up.

Reading the state

I need to read the state of the phototransistor, I think this will have the following steps:

  1. Power up the infra-red LED, using an analog out to achieve a specific voltage, or just using a digital out connected to a voltage divider to get the max 1.6 volts the LED can handle.
  2. The collector leg of the phototransistor will be connected to the positive battery, maybe directly, but maybe via a digital out so it can be switched on or off.
  3. Sample from a digital in, the emitter leg of the phototransistor, giving the reading.
  4. Power down the infra-red LED

Powering the LED

I need to supply a maximum of 1.6 volts to the infra-red LED according to the spec sheet, the digital outs on a JeeNode supply 3.3v, so using the trusty V = IR if I pop in a 100 ohm resistor in series with the LED that will ensure that the voltage is right for the IR LED.

Simple Debugging

To make sure my program and circuit worked I added an LED to the circuit, and made the program sample the state of the phototransistor every 250ms, and then write that state to the LED.

My first program

#include <JeeLib.h>

// This program does the following:
// 1. Turn on a digital out to illuminate an IR LED.
// 2. Read a value from a digital phototransistor
// 3. Put that state onto an LED on a different port.

#define PHOTOPIN   17   // AIO4
#define IRLEDPIN   5  // DIO2
#define DEBUGOUTPIN  6 // DIO3

int outputState;

MilliTimer scanTimer;

void setup() {
  // put your setup code here, to run once:
  
  // Setup our pins.
  pinMode(DEBUGOUTPIN, OUTPUT);
  pinMode(IRLEDPIN, OUTPUT);
  pinMode(PHOTOPIN, INPUT);
  
  outputState = false;
  
  // Show that we're ready to go
  digitalWrite(DEBUGOUTPIN, true);
  delay(50);
  digitalWrite(DEBUGOUTPIN, false);
  delay(50);
  digitalWrite(DEBUGOUTPIN, true);
}

void loop() {
  // put your main code here, to run repeatedly:
  
  if (scanTimer.poll(250)) {
    // Spin up the IR led.
    digitalWrite(IRLEDPIN, true);
    
    // Wait a brief moment.
    delay(1);
       
    // Read the value.
    outputState = digitalRead(PHOTOPIN);    

    // Turn off the IR LED.
    digitalWrite(IRLEDPIN, false);
    
    // Put the output state to our led.
    analogWrite(DEBUGOUTPIN, outputState & 127);
  }
}

I added a pull down resistor to the phototransitor collector, so that the digitalRead from it wouldn't get interference.

Results

It worked!

I pointed the phototransistor at my gas meter dial, and turned the heating on with my smartphone app, and as the dial turned to 0 the LED remained off, when the 0 was in front of the sensor, it lit up, and then when the 0 had gone past, the LED turned off.

Victory!

Next

Next up I'm going to clean up the program and the pin usage, and then I'm going to broadcast the count via the radio. Then I need to physically package up the sensor in a way that I can easily attach and detach from the meter, and also package up the JeeNode.

Written by Steven Jones on Saturday April 4, 2015
Permalink -

Leave a comment

comments powered by Disqus

« Mod Pagespeed - Spot counting - Broadcasting »