NPN Transistor Active Low help!

Hi guys.

newbie here. I can write simple programs but electronics not really my strong point.

I got my hands on a Alge Timing photocell and I've been trying to make it work with the Arduino.

I have push buttons to mimic a photocell passing but from the photocell itself I am not getting any info.

I'm planning to use the signal from the photocell to mimic a keypress. So far my code goes as follows:

#include <Keyboard.h>

int manualPassing = 3;  // photocell is here
int greenFlag = 9; // push-button is here

void setup()
{
  pinMode(manualPassing, INPUT_PULLUP);  // Set the button as an input
  pinMode(greenFlag, INPUT);
  digitalWrite(manualPassing, LOW); 
  digitalWrite(greenFlag, HIGH);
}

void loop()
{
  if (digitalRead(manualPassing) == HIGH) // I'm assuming it will be always HIGH as it is a NPN Transistor type
  {
    Keyboard.write('z');  
    delay(1000); 
    digitalWrite(manualPassing, LOW);
  }
  if (digitalRead(greenFlag) == LOW)  // if the button goes low
  {
    Keyboard.write('G');  
    delay(500); 
    digitalWrite(greenFlag, HIGH);
  }
}

I'm wiring it simply with common ground for both input and directly connecting to the pins.

Any help on how to receive the signal? I've tried with a PNP Type photocell and it works.

Thanks a bunch.

Regards!

To read an NPN open collector output, use pinMode(pin, INPUT_PULLUP). Don't forget to connect the grounds!

jremington:
To read an NPN open collector output, use pinMode(pin, INPUT_PULLUP). Don't forget to connect the grounds!

pinMode(manualPassing, INPUT_PULLUP);

I'm using that already.

When you say connect the ground, you mean the ground of the photocell to the ground of the arduino?

I'm using the Micro btw.

mazranz:
When you say connect the ground, you mean the ground of the photocell to the ground of the arduino?

Yes. Common ground and why you need one.

digitalWrite() to an input pin will not give you the results you expect.
It actually manipulates the pull up resistor.

Any advice then?

This is how it’s currently being wired up

https://m.imgur.com/gallery/hTqk9Zz

Delete the two instances of “digitalWrite(manualPassing, LOW);” and the program should work. If not, post a schematic of how it is connected to the sensor. Use the forum to post images, don’t expect people to use external websites, especially when you provide broken, non-clickable links.

Your link doesn’t work.
You are clearly trying to deliver only one simulated button press, no matter how long the input is brought low by your external device. You need to maintain your own flag to indicate that you have acted on a detection from the device. You set this flag when you have simulated a button press. You clear the flag when you no longer detect an input.

Post crossed with @WhatsThat

Delete the two instances of "digitalWrite(manualPassing, LOW);" and the program should work.

That doesn't work.

I've just wired it up like this. I'm not sure it is correct. but the button switch works as expected.

This is also incorrect:

if (digitalRead(manualPassing) == HIGH ) // I'm assuming it will be always HIGH as it is a NPN Transistor type.

It will be HIGH when there is no detection but for a detection you need to test if it is LOW.

Post your updated sketch if it still does not work.

I change to Serial Monitor for easier testing:

void setup
{
  digitalWrite(manualPassing, HIGH); 
}

void loop()
{  

  if (digitalRead(manualPassing) == LOW) 
  {
    Serial.println('Z');
    delay(1000);  
    digitalWrite(manualPassing, HIGH);
  }

produces

15:25:52.614 -> Z
15:25:53.637 -> Z
15:25:54.627 -> Z
15:25:55.616 -> Z
15:25:56.637 -> Z
15:25:57.618 -> Z
15:25:58.638 -> Z

even when there is nothing blocking the beam.

maybe I should get a multimeter and see the voltage output of the photocell?

It should be something more like:

int manualPassing = 3;  // photocell is here

void setup
{
  pinMode(manualPassing, INPUT_PULLUP );
}

void loop()
{ 

  if (digitalRead(manualPassing) == LOW)
  {
    Serial.println('Z');
    delay(1000); 
  }
}

If it doesn't work with the photocell trigger attached to pin 3, then try removing it and short pin 3 to ground to see if you get anything in the serial monitor.

The photocell does not output a voltage if it is an NPN output, it shorts a voltage to ground. You'd have to put one probe on Vcc (photocell) and the other on the output pin of the photo cell.

Success!!

Thanks for your help guys.

Have a good week ahead!