An optocoupler is a device that combines light-emitting and receiving devices in one package. Mainly, it combines the infrared light-emitting diode (LED) and a phototransistor.
There are three main types of optocouplers:
A symbol, sample optocoupler and its connection to the microcontroller are present in figures 1, 2 and 3.
An example code:
int optoPin = A0; //Initialize an analogue A0 pin for optocoupler int optoReading; //The analogue value reading from the optocoupler int objecttreshold = 1000; //Object threshold definition int whitetreshold = 150; //White colour threshold definition void setup () { //Begin serial communication Serial.begin(9600); //Initialise the analogue pin of the optocoupler as an input pinMode(optoPin, INPUT); } void loop () { optoReading = analogRead(optoPin); //Read the value of the optocoupler Serial.print ("The reading of the optocoupler sensor is: "); Serial.println(optoReading); //When the reading value is lower than the object threshold if (optoReading < objecttreshold) { Serial.println ("There is an object in front of the sensor!"); //When the reading value is lower than the white threshold if (optoReading < white threshold) { Serial.println ("Object is in white colour!"); } else { //When the reading value is higher than the white threshold Serial.println ("Object is in dark colour!"); } } else { //When the reading value is higher than the object threshold Serial.println ("There is no object in front of the sensor!"); } delay(500); //Short delay }
This type of sensor gives information about the colour of the light illuminating the sensor surface. Because computers often use RGB (red, green, blue) colour schemes, the sensor returns three values representing the intensity of three components. Colour sensors usually contain white LEDs to illuminate the surface, which colour should be distinguished by them. The colour sensor uses an SPI or TWI interface to send readings. Some models of colour sensors include an additional gesture detector which recognises simple gestures (up, down, left, right).
The sample device is present in figure 4 and the connection circuit in figure 5.
#include <Wire.h> #include "Adafruit_TCS34725.h" // Example code for the TCS34725 library by Adafruit // Sensor class Adafruit_TCS34725 rgb_sensor = Adafruit_TCS34725(); void setup(void) { Serial.begin(9600); Wire.begin(5,4); //SCL SDA pinMode(21, OUTPUT); //Pin 21 controls LED digitalWrite(21,LOW); //Turn off onboard LED if (rgb_sensor.begin()) { //Initialise RGB sensor Serial.println("RGB sensor present"); } else { Serial.println("No TCS34725 found"); while (1); } } void loop(void) { uint16_t r, g, b, unfiltered, lux; rgb_sensor.getRawData(&r, &g, &b, &unfiltered); //read RGB and unfiltered light intensity lux = rgb_sensor.calculateLux(r, g, b); //calculate illuminance in Lux Serial.print("Lux: "); //print calculated Lux value Serial.print(lux, DEC); Serial.print(" - "); Serial.print("R: "); //print red component value Serial.print(r, DEC); Serial.print(" "); Serial.print("G: "); //print green component value Serial.print(g, DEC); Serial.print(" "); Serial.print("B: "); //print blue component value Serial.print(b, DEC); Serial.print(" "); Serial.print("C: "); //print unfiltered sensor value Serial.print(unfiltered, DEC); Serial.println(" "); delay(1000); }