This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot-open:hardware2:sensors_optical [2023/08/25 12:10] – ekontoturbo | en:iot-open:hardware2:sensors_optical [2023/11/23 12:38] (current) – pczekalski | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Optical Sensors ====== | ||
| + | {{: | ||
| + | == 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**, | ||
| + | |||
| + | A symbol, sample optocoupler and its connection to the microcontroller are present in figures {{ref> | ||
| + | <figure optocoupler1> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | <figure optocoupler2> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | <figure optocoupler3> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | An example code: | ||
| + | <code c> | ||
| + | int optoPin = A0; // | ||
| + | int optoReading; | ||
| + | |||
| + | int objecttreshold = 1000; //Object threshold definition | ||
| + | int whitetreshold = 150; // | ||
| + | |||
| + | void setup () | ||
| + | { | ||
| + | //Begin serial communication | ||
| + | Serial.begin(9600); | ||
| + | // | ||
| + | pinMode(optoPin, | ||
| + | } | ||
| + | |||
| + | void loop () | ||
| + | { | ||
| + | optoReading = analogRead(optoPin); | ||
| + | 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 (" | ||
| + | //When the reading value is lower than the white threshold | ||
| + | if (optoReading < white threshold) { | ||
| + | Serial.println (" | ||
| + | } else { //When the reading value is higher than the white threshold | ||
| + | Serial.println (" | ||
| + | } | ||
| + | } | ||
| + | else { //When the reading value is higher than the object threshold | ||
| + | Serial.println (" | ||
| + | } | ||
| + | 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> | ||
| + | |||
| + | <figure tcs1> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | <figure tcs2> | ||
| + | {{ : | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | <code c> | ||
| + | #include < | ||
| + | #include " | ||
| + | |||
| + | // 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, | ||
| + | pinMode(21, OUTPUT); | ||
| + | digitalWrite(21, | ||
| + | if (rgb_sensor.begin()) { // | ||
| + | Serial.println(" | ||
| + | } else { | ||
| + | Serial.println(" | ||
| + | while (1); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | void loop(void) { | ||
| + | uint16_t r, g, b, unfiltered, lux; | ||
| + | |||
| + | rgb_sensor.getRawData(& | ||
| + | // | ||
| + | lux = rgb_sensor.calculateLux(r, | ||
| + | // | ||
| + | |||
| + | Serial.print(" | ||
| + | Serial.print(lux, | ||
| + | Serial.print(" | ||
| + | | ||
| + | Serial.print(" | ||
| + | Serial.print(r, | ||
| + | Serial.print(" | ||
| + | | ||
| + | Serial.print(" | ||
| + | Serial.print(g, | ||
| + | Serial.print(" | ||
| + | | ||
| + | Serial.print(" | ||
| + | Serial.print(b, | ||
| + | Serial.print(" | ||
| + | | ||
| + | Serial.print(" | ||
| + | Serial.print(unfiltered, | ||
| + | Serial.println(" | ||
| + | |||
| + | delay(1000); | ||
| + | } | ||
| + | </ | ||