====== Optical Sensors ======
{{:en:iot-open:czapka_b.png?50| General audience classification icon }}{{:en:iot-open:czapka_e.png?50| General audience classification icon }}\\
== Optocoupler ==
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:
* an **optocoupler of a closed pair configuration** is enclosed in the dark resin and is used to transfer signals using light. This type of optocoupler is not a sensor itself but is used for ensuring electrical isolation between two circuits;
* a **slotted optocoupler** has an open space between the light source and the sensor; external objects can obstruct light and thus can influence the sensor signal. It can be used to detect the presence of flat objects, measure rotation speed, vibrations or serve as a bounce-free switch;
* a **reflective pair configuration**, the light signal is perceived as a reflection from the object's surface. This configuration is used for proximity detection, surface colour detection and tachometer.
A symbol, sample optocoupler and its connection to the microcontroller are present in figures {{ref>optocoupler1}}, {{ref>optocoupler2}} and {{ref>optocoupler3}}.
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
}
== Colour Sensor ==
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 {{ref>tcs1}} and the connection circuit in figure {{ref>tcs2}}.
#include
#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);
}