Guide/ help Audrino based counter

Hi @ll

first off, audrino and co. is a totally new subject for me (noob) and I am hopping for some help/assistance to get started.
I want to create a simple audrino based counter for every time a item passes a capacitive sensor.
Link to sensor: Kapazitiver Sensor CBB4-12GH60-E2-V1

The counter will be a GUI running on Linux based either on PyQt4 or with Gambas, if a item passes the sensor i am thinking the IO would give me some signal then I just have to grab it and "value = value +1".. (It is a simple task I guess, but I really don't know where to start with audrino..)
So my question is, if this is possible with audrino how would I read the data from the audrino board with that type of sensor?
What audrino board would be best for that?

I am thankful for any help.

Cheers..

That sensor appears to be a normally open switch. When there is something in the vicinity of the sensor, it changes from open to closed.

The only difficulty reading the voltage when the switch is closed is that the sensor operates on 10 to 36 volts, so, presumably, there will be 10 to 36 volts on the output leg when the switch closes. A suitable voltage divider would drop that to the 5V that the Arduino can handle.

The Arduino would then send serial data to the PC. That could be a count, or just "Ding!". The PC would need to be reading the serial port, and doing something with the incoming data.

Any Arduino would be suitable.

Thanks for your reply..
I got everything setup for my experiment, and did some testing but I ran in to another problem.

The board I am using is a Ardunio Duemilanove ATmega168
I have the (4pin) sensor on a external electric source (27V), when the sensor closes I get about 1V on the input pin (3), so that should do and not blow my arduino .

I wrote a program that looks like this:

int val = 0;

void setup()
{     
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  pinMode(3, INPUT);
  
  Serial.println("Connected");
}

void loop()
{
  val = digitalRead(3);
  digitalWrite(13, val);
  Serial.println(val);
}

the serial output gives me 000111010011101011111010010010100101
So the LED is blinking very fast, even if I dont have the sensor hocked up.
If I hookup the sensor the same thing, i dont get any usable output or a open close result..

Any suggestions? I read something in the net about callibrating?
Thanks for the help.

When the sensor isn't connected, the pin is floating - it will vary randomly, as you observe.

The fact that it does the same when the sensor is hooked up suggests that you haven't connected the grounds of the two devices together.

1V isn't going to cut it - the Arduino will perceive that as LOW. Needs to be at least, IIRC, 3.5V, you will need some other hardware to amplify it.

you could use a suitable pull down resistor, and take an analog reading, and then you can detect the 1v easily, even easier if you use 3.3v as the reference on the aref pin

wildbill:
When the sensor isn't connected, the pin is floating - it will vary randomly, as you observe.

The fact that it does the same when the sensor is hooked up suggests that you haven't connected the grounds of the two devices together.

1V isn't going to cut it - the Arduino will perceive that as LOW. Needs to be at least, IIRC, 3.5V, you will need some other hardware to amplify it.

hmmm, okay i tested that with the 3.3V outage on the arduino, plugged it in to the input(3).. And that looks good..
Now my serial output gives me clean 111111111111111111111111111 and 000000000000000000000000 :slight_smile:
makes sense now..
Thanks alot...
now I got to look how I can transform 1V to 3.3V somehow..

winner10920:
you could use a suitable pull down resistor, and take an analog reading, and then you can detect the 1v easily, even easier if you use 3.3v as the reference on the aref pin

I dont quite get it? Should I use Analog in?
Or do you mean something like this?
http://sebastianpatten.wordpress.com/2011/02/20/netduino-pull-up-and-pull-down-resistor/

I just orderd me some hardware where I can amplify my voltage.. it cost me 80EURO's .. hope that works

With a pull down resistor and analogRead() of that you can't detect the 1v
analog read gives you a value 1-1024(0-5v) by default, you can change it to 0-3.3v easily by selecting external aref and using the 3.3v pin
so when the device is putting out a 0 you will get a number low 0-15, and when it putting out a 1 you will read around 250(5v ext reference) or 310(3v ext reference), you can then say
if(analogRead(A0) > 200) {
sensortripped = 1;
}

All you need to do is wire the output to pin A0 instead of a digital pin, much cheaper id say, although it is slower than a digital read

Thank's winner10920, awsome that did the trick..

I hooked the sensor power on the 9V pin and connected GND and the signal on A03, even though I am under powering the sensor it works and I can use the analogin to fetch the signal.

Thinking about how to write a counter on the board, when the sensor closes it will show 11111111111111111111111, when it opens it will 0000000000000000..
Which is great...
If I write count = count + 1, then it will count up as long as the sensor is closed (active) .. But I want to count 1 once as long as the sensor state is closed and send 1 over the serialport and not 1111111111.

There will also be a LCD display that will display the count result.

Now, either I do this over in the Gambas GUI, then I have to Read and Write back over the serialport to display it on the LCD (which I don't want to), or I do this on the Arduino directly if possible? and send the end result to my program..
Im not that good in C ... Would appreciate any help..

int val = 0;
int sensoractive = 0;
int counter = 0;

void setup()
{     
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  pinMode(A3, INPUT);
  
  Serial.println("Connected");
}


void loop()
{
  val = analogRead(A3);
  
  if(val > 130){
    sensoractive = 1;
    digitalWrite(13, 1);
    Serial.println(sensoractive);
  }
  else
  {
  sensoractive = 0;
    digitalWrite(13,0);
    Serial.println(sensoractive);
  }
  
}

You can probably do it on the arduino depending on the lcd, most work
,make a variable that is triggered when the value changes, then do nothing until there is a change

winner10920:
You can probably do it on the arduino depending on the lcd, most work
,make a variable that is triggered when the value changes, then do nothing until there is a change

Yea, that's what I was looking for, but did'nt know how.. I found tutorial on the Arduino page and rewrote it to this, and it works so far:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);

const int sensorPin = A3;
const int ledPin = 13;

int sensorTriggerdCounter = 0;
int sensorState = 0;
int lastSensorState = 0;


void setup() { 
  pinMode(ledPin, OUTPUT);
  lcd.init(); 
  lcd.backlight();
  lcd.print("Items : ");
  lcd.print("0");
  Serial.begin(9600);
}

void loop()
{
  //read the analog singlnal
  sensorState = analogRead(sensorPin);
  
  //translate analog value to a usable statment
  if (sensorState > 120) {
    sensorState = 1;
  }
  else {
    sensorState = 0;
  }
  // compare the current state to its previous state
  if (sensorState != lastSensorState) {
    // if the state has changed, increment the counter
    if (sensorState == HIGH) {
      // if the current state is HIGH (1) then boolen
      // went from 0 to 1 (off/on)
      sensorTriggerdCounter++; //Count
      lcd.clear();
      lcd.print("Items : ");
      lcd.print(sensorTriggerdCounter);
      Serial.println(sensorTriggerdCounter);
      digitalWrite(ledPin, HIGH);
    }
    else {
      //
      digitalWrite(ledPin, LOW);
    }
  }
  // save the current state as the last state,
  //for next time through the loop
  lastSensorState = sensorState;
}

IMAGE of my project:

Uploaded with ImageShack.us

Looks nice, btw what case is that? Looks nice

winner10920:
Looks nice, btw what case is that? Looks nice

thanks..

I bought it on the internet.... here: http://www.mercateo.com/p/139-1848692/BOX_FOR_ARDUINO.html
But if you google for arduino case, you will find it .. like here : http://www.antratek.com/Enclosures.html

Do you (or anyone) know how to send data over the serialport and assign it strait to a variable?
I am trying it with serial.read() but it don't work, or I am too stupid.. :wink:
Also tried it with a switch/case, but no success..
I want to send a number, assign it in the arduino to a variable and display it on the LCD. It will be a TargetNumber.
Then check if the counted number equals the targetnumber.. If (!target == counted) then stop..

If i get this figured, then I have the internal basics done and I can start moving on to gui programming ..

What is probably your problem is that you are trying to comapare the ascii 1 with the number 1, or similar
so serial.read will return a '1' which != 1, '1' actually equals 49 , or the decimal equivalent to the ascii character
you need to subtract 48 from the ascii number value to get an actual value
also you should ignor the invisible charcters that may come like line feed ( decimal 10, or 13 I forget)

So this will work for a number 0-9
If(serial.available() > 0){
Incoming = serial.read();
Targetvalue = incoming - 48;
}
now for numbers taking up multiple places you wont get 0123 over serial but '0','1','2','3' which equals, 48,49,50,51 , you will need to account for the place value yourself by taking the hundreds digit x 100, tens x 10, and add the last digit
which means that 0123 == ('0'-48) * 1000 + ('1' - 48) * 100 + ('2' - 48) * 10 + ('3' - 48)
hope that helps a little,