Pages: [1]   Go Down
Author Topic: phototransistor help  (Read 1052 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

thanks
Logged

0
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2857
ruggedcircuits.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
Shannon Member
****
Karma: 200
Posts: 11730
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
Logged

[ I won't respond to messages, use the forum please ]

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 CAM_TRIGGER_PIN 4
#define FLASH_TRIGGER_PIN 5
#define TRIGGER_PIN 14
#define LED_PIN 13
#define STANDBY 0
#define ACTIVE 1
#define WORKLIGHT_RELAY 6
#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(TRIGGER_PIN, INPUT);
pinMode(UP_PIN, INPUT); 
pinMode(DOWN_PIN, INPUT);
pinMode(RESET_PIN, INPUT);
pinMode(BUTTON_PIN, INPUT);
pinMode(CAM_TRIGGER_PIN, OUTPUT);
pinMode(FLASH_TRIGGER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(WORKLIGHT_RELAY, OUTPUT);
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
{
delay(flashDelayMS);
digitalWrite(FLASH_TRIGGER_PIN, HIGH); // fire flash
delay(50);
digitalWrite(FLASH_TRIGGER_PIN, LOW);
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.setCursor(0,3);
  lcd.print(flashDelayMS); //show current delay time
}
}
Logged

0
Offline Offline
Shannon Member
****
Karma: 200
Posts: 11730
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
Logged

[ I won't respond to messages, use the forum please ]

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ...
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
 //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);
  Serial.begin(9600);
}

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);
}  
Logged

Pages: [1]   Go Up
Jump to: