Simple circuit/sketch not working

Hi

Here is my simple circuit:


5V are obtained from 12V using linear regulator 7805.

Here is the sketch:

#define output 0 // Pin 5
#define sensorInput 1 // Pin 6
#define countingPOT 4 // Pin 3
#define durationPOT 3 // Pin 4

int counts;
int strokeCount = 0; 
int Duration = 1000;

void setup() 
{
  pinMode(sensorInput, INPUT);
  pinMode(output,     OUTPUT);
}

void loop()
{
//  counts = analogRead(countingPOT);
  counts = 10;//map(Count, 0, 1023, 2, 10);
//  Duration = analogRead(durationPOT);
//  Duration = map(Duration, 0, 1023, 100, 1000);
  
  if (digitalRead(sensorInput) == HIGH) // Stroke
  { 
    strokeCount++;
  }
  if (strokeCount > 9)
  {
    digitalWrite(output, HIGH);
    delay(Duration);
    digitalWrite(output, LOW);
    strokeCount = 0; 
  }
}

Whenever the proximity sensor is ON strokeCount is increased by 1. When the strokeCount is 10, a relay is activated thru an NPN transistor for a period specified by the variable Duration and then the variable strokeCount is intialized.

However, physically the code is not performing as expected. The relay is being activated on every pulse received from the sensor. I have tried various changes but nothing improved. It seems that the value of digitalRead(sensorInput) is always HIGH which makes the 2nd IF condition always TRUE. But, the voltage at pin 6 is always LOW when sensor is OFF.

I can't figure out where am I wrong.

For switches, look at when there is a change in state not the switches level.

Thank you for responding so quickly.

I am using a 3-wire inductive proximity sensor, not a push button or switch.

Same difference.

Don’t look at the level, look at the change in state.


counts = 10;

Why ?


BTW

A simple transistor can be used in place of the Opto. :thinking:

Which acts exactly like a switch or pushbutton, in the posted circuit.

I'm no expert but I think that opto-isolator has an Open Collector output and it is wired incorrectly. You would put the pull-up resistor between the upper terminal (Collector?) and +5V. Connect the upper terminal also to the ATtiny input pin. Connect the lower terminal (Emitter?) directly to Ground.

No, that wiring is fine. Works either way, but with opposite logic states.

Is your proximity sensor wired like this?

  • Blue - GND
  • Brown - 12V
  • Black - optocoupler

Can you share the proximity sensor part number?

If that really is an open-collector sensor (NPN), then the opto-isolator is serving no purpose. Connect the output lead directly to the tiny85 and pull the input pin high with that 4.7k resistor, or use the INPUT_PULLUP configuration.

Next thing is to be sure that nothing is triggering the sensor and see if you get the same behavior. Normally I'd say print something out the serial port, or flash the built-in LED when the sensor is triggered, but this isn't a regular Arduino!

Correct.

However, the OP needs to tell us if there is significant distance between the sensor and the Arduino.

Long distance would mean the Arduino input pin would be connected to an antenna :woozy_face: .

If that’s the case, a transistor back at the Arduino would be prudent.

Correct

Yes, the sensor cable is about 3 meter long.

Yes, the problem is resolved by considering the "change in state".

Counts = 10, is my requirement. I want to activate the relay after 10 signals.

Yes, a simple NPN could also do the job. For me, both cost the same. :sunglasses:

Finally, I am thankful to all the participants for helping me.