Go Down

Topic: phototransistor help (Read 2273 times) previous topic - next topic


I am pretty new to arduino and have no idea how phototransistors work but what i want to do it use a phototransistor to trigger something to happen. so when there is no ir light something will happen. i have a basic NPN phototransistor from radioshack and im wondering if i wire it up more or less like in this picture http://www.flickr.com/photos/anachrocomputer/3483847795/#/ can i more or less use it like a button with a line of code that says something like " if (digitalread(PHOTOTRANSISTOR) == LOW); then.......

any help or direction to a tutorial would be greatly apreciated



Yes you can more or less do it this way. If the phototransistor sees no light, your digitalRead() should return LOW. If it sees light, it should return HIGH. For a resistor value I would use about 10k.

You can also take a look at the way we hooked up a phototransistor in our Gadget Shield (see the schematic, page 3, the part marked Light Sensor). If you omit R18 and R21 you can just hook up the phototransistor right to a digital pin but then you have to enable the pull-up resistor.

The Aussie Shield: breakout all 28 pins to quick-connect terminals


Yes it should work - you will have to select the right resistor value for your particular phototransistor / light level by trial and error (but not below about 150ohms to prevent damage to the phototransistor (assuming it can take 30mA or so).

The circuit you link too uses an analog pin - using analogRead() on an analog pin gives you a direct measure of brightness - but if you only want a button-replacement digitalRead() ought to work (be aware you may need to debounce the signal just like a physical switch).

Incidentally all semiconductors are photo-sensitive, "phototransistors" are ones that aren't encapsulated in opaque material (and optimized for light sensistivity)
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]


so that did work I will share my code and maybe someone could look it over and see if it is a problem with the code, i tried a 1k resistor a 10k resistor and a 1m resistor. also unrelated but i have never done anything with LCDs but i wrote this code to include an lcd display, if someone could maybe tell me if that part of the code is good too that would be great, i dont actually have an lcd display yet to try it with.

// original code by Matt Richardson added onto by P@ Doherty
// high speed photography rig with variable delay and display
//triggered by photo transistor
#define BUTTON_PIN 18
#define UP_PIN 15
#define DOWN_PIN 16
#define RESET_PIN 17
#define TRIGGER_PIN 14
#define LED_PIN 13
#define STANDBY 0
#define ACTIVE 1
#include <LiquidCrystal.h>
int mode = STANDBY;

// For best results, set flashDelayMS according to what type
// of shot you're doing. 0 seems best for balloon burst while
// 10 seems best for shattering glass. YMMV.
long flashDelayMS = 10;

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);

void setup() {
pinMode(UP_PIN, INPUT); 
digitalWrite(LED_PIN, HIGH);
digitalWrite(WORKLIGHT_RELAY, HIGH); //turn the lights on
  lcd.begin(16, 2);
  lcd.print("Created by P@ Doherty");

void loop() {
if (digitalRead(BUTTON_PIN) == HIGH)
mode = ACTIVE;
digitalWrite(WORKLIGHT_RELAY, LOW); // turn the lights off
delay(2000); // to give time for light to go down and settle after button push
digitalWrite(LED_PIN, LOW); // show we're ready
digitalWrite(CAM_TRIGGER_PIN, HIGH); // open the camera shutter
if ((mode == ACTIVE) && (digitalRead(TRIGGER_PIN == LOW))); // if it is in active mode and the light gate is broken
digitalWrite(FLASH_TRIGGER_PIN, HIGH); // fire flash
digitalWrite(CAM_TRIGGER_PIN, LOW); // close camera shutter
mode = STANDBY;
digitalWrite(LED_PIN, HIGH);
digitalWrite(WORKLIGHT_RELAY, HIGH); // turn lights back on
if (digitalRead(UP_PIN) == HIGH)
  flashDelayMS = flashDelayMS + 1; // add 1 MS to delay time
if (digitalRead(DOWN_PIN) == HIGH)
  flashDelayMS = flashDelayMS - 1; // subtract 1 MS to delay time
if (digitalRead(RESET_PIN) == HIGH)
  flashDelayMS = 10; // redet delay time to 10 MS or any time desired
  lcd.print(flashDelayMS); //show current delay time


You should use the code tag for code samples (the # button above when composing reply).

Code: [Select]
if ((mode == ACTIVE) && (digitalRead(TRIGGER_PIN == LOW))); // if it is in active mode and the light gate is broken

This is clearly wrong as (TRIGGER_PIN == LOW) returns a boolean, not a pin number.  One of your parentheses is in the wrong place.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]


sorry about the code thing. so i changed the parentheses to this and it is still not working and i tried different resistor values and still no good.
Code: [Select]
if ((mode == ACTIVE) && (digitalRead(TRIGGER_PIN) == LOW))
the strange thing is though that even with just the trigger_pin hooked up to ground with a pull down transistor and no phototransistor hooked up the LED just turns off for a split second then turns back on


i suppose i could use an ldr (Cds photoresistor whatever you want to call it) and maybe a laser with a if < threshold line of code? if anyone knows of a good tutorial for something like that let me know like i said im new to arduino and coding so ...


I'm new to both electronics and programming and currently doing a very similar project for high speed flash photography.
All i have sorted at the moment is the laser circuit and the receiver circuit which uses a phototransistor.
I'm using 200k resistance with my photo transistor which triggers when the laser beam aligned with it is broken.
Here is my code with thanks to AWOL.
Play with trigger threshold and resistors to get the best response time for the flash.
Code: [Select]
 //Light sensitive flash trigger using a phototransistor
 //when sensor is momentarily blocked of light, analogue read
 //increases which will output 5v to an optocoupler for 1 second
 //then delays for 1 seconds.

 //optocoupler + connected to digital pin 13
 //phototransistor connected to analogue pin 0
#define optoPin 12
#define triggerPin A0
#define delayTime 0
#define triggerThreshold 300
#define triggerTime 1000

void setup()
 //initialize the digital pin 13 as an output.
 pinMode(optoPin, OUTPUT);

void loop()
 int sensorValue = analogRead(A0);
 Serial.println(sensorValue, DEC);
 while (analogRead (triggerPin) < triggerThreshold);
 delay (delayTime);
 digitalWrite (optoPin, HIGH);
 delay (triggerTime);
 digitalWrite (optoPin, LOW);
 delay (1000);

Go Up