CJMCU 101 OPT101 Analog light sensor with Arduino

Hello, welcome back. In this tutorial, we will learn what is the CJMCU 101 analog light sensor and how it works with an Arduino. Also, a light value detection project has been used to explain the functionality of this sensor. The Arduino Nano board is used for that purpose and five LEDs are used to display the outputs. We have previously used the LDR sensor to detect the amount of light value, and this CJMCU 101 sensor can be used instead of the LDR sensor. That is, this sensor can be used for all projects designed using the LDR sensor.

What is the CJMCU 101 sensor?

This sensor module is created using the OPT101 light sensor chip. Also, this OPT101 chip includes a monolithic photodiode and a transimpedance amplifier. We can get the light intensity as the Analog values through this sensor. Also, this chip requires a potential of 2.7v to 36v for operation and it can be operated by a single or dual power supply.

Various applications that use this sensor

  • Barcode scanners.
  • Smoke detectors.
  • Proximity sensors.
  • Medical instrumentation.

Internal structure of this sensor

In this tutorial, we will use the CJMCU 101 sensor module, which includes the OPT101 light sensor chip.

The pin structure of this sensor module

Okay, let’s learn how this sensor works with an Arduino. The required components are as follows.

Step 1

Firstly, identify these components.

Step 2

Secondly, solder the path as in the picture below.

Step 3

Thirdly, connect these components. To do this, use the circuit diagram below.

Step 4

Then, let’s creates the program for this project. It is as follows.

  • The complete program of this project – Download
/*CJMCU 101 sensor with Arduino
 * https://srituhobby.com
 */
 
#define sensor A2
#define LED1 2
#define LED2 3
#define LED3 4
#define LED4 5
#define LED5 6

void setup() {
  Serial.begin(9600);
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
}

void loop() {
  int value = analogRead(sensor);
  value = map(value, 0, 1024, 300, 1100);
  Serial.println(value);

  if (value < 310) {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
    digitalWrite(LED5, LOW);
    Serial.println("LED off");
  } else if (value > 310 && value < 350) {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
    digitalWrite(LED5, LOW);
    Serial.println("LED 1 on");
  } else if (value > 350 && value < 450) {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
    digitalWrite(LED5, LOW);
    Serial.println("LED 2 on");
  } else if (value > 450 && value < 550) {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    digitalWrite(LED4, LOW);
    digitalWrite(LED5, LOW);
    Serial.println("LED 3 on");
  } else if (value > 550 && value < 750) {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    digitalWrite(LED4, HIGH);
    digitalWrite(LED5, LOW);
    Serial.println("LED 4 on");
  } else if (value > 750) {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    digitalWrite(LED4, HIGH);
    digitalWrite(LED5, HIGH);
    Serial.println("LED 5 on");
  }

}

Code explanation

Firstly, the sensor pin and LEDs pins are defined.

#define sensor A2
#define LED1 2
#define LED2 3
#define LED3 4
#define LED4 5
#define LED5 6

In this setup function,

void setup() {
//The serial monitor starts
  Serial.begin(9600);
//The LED pins are configured as output pins
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(LED4, OUTPUT);
  pinMode(LED5, OUTPUT);
}

In this loop function,

void loop() {
//Gets the sensor values
  int value = analogRead(sensor);
//Converts these values from 300 to 1100
  value = map(value, 0, 1024, 300, 1100);
  Serial.println(value);
//These values are checked using the IF condition and LED turns ON and OFF
  if (value < 310) {
    digitalWrite(LED1, LOW);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
    digitalWrite(LED5, LOW);
    Serial.println("LED off");
  } else if (value > 310 && value < 350) {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, LOW);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
    digitalWrite(LED5, LOW);
    Serial.println("LED 1 on");
  } else if (value > 350 && value < 450) {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, LOW);
    digitalWrite(LED4, LOW);
    digitalWrite(LED5, LOW);
    Serial.println("LED 2 on");
  } else if (value > 450 && value < 550) {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    digitalWrite(LED4, LOW);
    digitalWrite(LED5, LOW);
    Serial.println("LED 3 on");
  } else if (value > 550 && value < 750) {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    digitalWrite(LED4, HIGH);
    digitalWrite(LED5, LOW);
    Serial.println("LED 4 on");
  } else if (value > 750) {
    digitalWrite(LED1, HIGH);
    digitalWrite(LED2, HIGH);
    digitalWrite(LED3, HIGH);
    digitalWrite(LED4, HIGH);
    digitalWrite(LED5, HIGH);
    Serial.println("LED 5 on");
  }
}

Step 5

OK, now select board and port. Afterwards, upload this code to the Arduino board.

OK, enjoy this project. The full video guide is given below. So, we will meet in the next tutorial.

CJMCU 101 OPT101 Analog light sensor with Arduino

Leave a Reply

Your email address will not be published. Required fields are marked *