CNY70 Reflective Optical Sensor for Teensy 3.2

Status
Not open for further replies.

RodCar

New member
Hello, all.

This is my first post to the PJRC forums and you all seem quite helpful. This is my first time working with teensy and am a little lost on a few of the technicalities that come with these devices. My main goal is to be able to detect fluorescent paint markings as it passes my optical sensor, which emits and receives emitted infrared light. With alternating current, it provides a reading. I've found a code for this same sensor via youtube (link to the video provided at the bottom of the code). The young man demonstrates his code working with a black box. The only exception is that his code is to detect and count the number of times this black block passes the sensor using an ARDUINO ONE. This is a perfect start for what I intend to do in terms of building blocks of the code.

My issues begin with fully understanding the code and what it references. I also am curious about how to go about using the correct pins for data input on my teensy. Directly below is the github in which the original code is from. I've translated from spanish to english for this post's sake for the included code.

I was hoping to gain some clarity as to why this specific code wouldn't work for a teensy. When trying to run the code, something I've noticed off the bat is the lack of the Dhcp.h and Dns.h libraries on the teensy. Will I have to manually upload these libraries to the teensy for this code to work, or is there a way around this issue?

I'm hoping this can be a fun and efficient learning experience for teensy and arduino code compatibility, as well as learning the ins and outs of microcontrollers.

Thanks for any help in advance,

Rod



https://gist.github.com/abimaelajtun/967d97e4bf0f734d3780#file-sensor_ncy70-ino

#include <Dhcp.h>
#include <Dns.h>
#include <ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>

// Pin used for LED output
Const int led = 8;
// Pin used for NCY70 sensor input
Const int in_infrarrojo = 4;
// Variable used to store 0 or 1, where 0 = Black and 1 = Any other color
Int val_infrarrojo = 0;
// Variable in charge of counting boxes
Int counter = 0;


Void setup () {
// Set the led pin as output
PinMode (led, OUTPUT);
// We set the infrared pin as input
PinMode (in_infrared, INPUT);
// Establish serial communication at 9600 baud
Serial.begin (9600);
// We show the initial value of the counter (Can be displayed in the Serial Monitor)
Serial.print ("Number of Boxes:");
Serial.println (counter);
}

Void loop () {
// Read the value provided by the infrared sensor
Val_infrarrojo = digitalRead (in_infrarrojo);
// We validate if the sensor detects some object of black color
If (val_infrarrojo == 0) {
// If a black object is detected we turn off the led
DigitalWrite (led, 0);
// Until another color other than black is detected again
While (val_infrarrojo == 0) {
// If red_value does not change value, ie zero, it means that the object is still below the sensor
Val_infrarrojo = digitalRead (in_infrarrojo);
}
// It will leave the cycle when the sensor detects a color other than black, ie non-zero
// We increase the value of the counter
Counter ++;
// We show the number of boxes that have passed under the sensor
Serial.print ("Number of Boxes:");
Serial.println (counter);
} Else {// Otherwise the led will be on
DigitalWrite (led, 1);
}
}


Via YouTube https://www.youtube.com/watch?v=sIkA_b5_lmc
 
Last edited:
Take out the 6 #includes, they are for ethernet you may not need, certainly not for this test.

Any digital pin on teensy should work.

That is running on arduino, I assume 5v through resistor to power the emitter led. You may need a smaller resistor to lower from 3.3v teensy

That's what I see now on my phone, that and you might want a while (!Serial && micros () < 4000) ; after serial begin to make sure the prints are there when The SerMon connects
 
Take out the 6 #includes, they are for ethernet you may not need, certainly not for this test.

Any digital pin on teensy should work.

That is running on arduino, I assume 5v through resistor to power the emitter led. You may need a smaller resistor to lower from 3.3v teensy

That's what I see now on my phone, that and you might want a while (!Serial && micros () < 4000) ; after serial begin to make sure the prints are there when The SerMon connects

We have connected the optical sensor and are getting a proper readout now. 5V into the emitter, and we are using the 3.3V out of the regulator of the Teensy to power the receiver. Our biggest issue was not using the analog ground on the teensy. Simple circuitry issues.

But, we are merely getting an output of values between 0 and 1023 given the following code.

Otherwise, the code from the original post, while excluding the #includes, is still having problems. I'll be back to edit this post with the code that is giving us and output, and also the error message for the OP code.
 
Status
Not open for further replies.
Back
Top