Hi,
Im trying to create and code a photo-transistor sensor (TCRT5000L) to become a motor encoder.
I've created my own notched spin-wheel that is attached to the motor. I'm using an arduino
nano and then sending the data via X-bee. When my motor is spinning I am getting an
appropriate signal on the transistor side however its only 2 volts. This is fine because x-bee's only accept signals of 1.2V
so I will use an appropriate voltage divider.
I found some simple code on a website and was wondering if its possible to make the digital input accept 2 Volts or if it has to be 5 Volts.
As of right now my transistor sends out 2V but with this code loading I get no signal on the pulse output. This code seems simple but I think it should work, maybe can somebody confirm?
Thanks in advance!
const int encoderIn = 11; // input pin for the interrupter
const int statusLED = 2; // Output pin for Status indicator
const int pulseOutput = 4; // Pulse output pin for external interfacing
int detectState=0; // Variable for reading the encoder status
void setup()
{
pinMode(encoderIn , INPUT); //Set pin 8 as input
pinMode(statusLED, OUTPUT); //Set pin 13 as output
pinMode(pulseOutput, OUTPUT); // Set Pin 12 as output
}
void loop() {
detectState=digitalRead(encoderIn);
if (detectState == HIGH) { //If encoder output is high
digitalWrite(statusLED, HIGH); //Turn on the status LED
digitalWrite(pulseOutput,HIGH); // Give a logic-High level output
}
else {
digitalWrite(statusLED, LOW); //Turn off the status LED
digitalWrite(pulseOutput,LOW); // Give a logic-Low level output
}
}
Are you using the appropriate input/output pins? For example in the first line of your code encoderIn is set to 11 but within the setup function your comment says "//Set pin 8 as input".
As of right now my transistor sends out 2V but with this code loading I get no signal on the pulse output.
2V is not sufficient to be detected as a high on a digital input. You could read it using analogRead(), but that would be slow. What is the value of the collector resistor on the phototransistor?
Or, try something like the following circuit, which will amplify and invert the signal. Depending on light levels and photodiode used, you may need to change R1 (try 50K - 3.3Meg).
Thank you guys,
Archibald - That was my silly mistake.
jremington - I've implemented this.
Now the only thing that I need is some way of averaging the values, doing some math to calculate the RPM.
How could I take a sample for say 10ms or 20 ms and for every pulse add them to a variable then clear it after my sample time.
Logically it seems simple, but since code is executed sequentially my brain is having a hard time thinking of a way to make it work.
Im trying to create and code a photo-transistor sensor (TCRT5000L) to become a motor encoder.
I've created my own notched spin-wheel that is attached to the motor.
What you have is a reflective optical sensor. This is better suited for a wheel with reflective and black markings.
A photo interrupter is better suited for a notched wheel.
Anyways, with your reflective optical sensor, I'm quite sure you could get a stronger signal (0-5V) on the emitter with this simple circuit. For use with the X-bee, just connect the collector to 1.2V power and the signal would become 0-1.2V.
Orchid0:
Now the only thing that I need is some way of averaging the values, doing some math to calculate the RPM.
How could I take a sample for say 10ms or 20 ms and for every pulse add them to a variable then clear it after my sample time.
Logically it seems simple, but since code is executed sequentially my brain is having a hard time thinking of a way to make it work.