# Light SensorA

{% embed url="<https://youtu.be/3KiVuz6DP2U>" %}

## Function introduction

The sensor integrates two photoresistors (depending on the light intensity adjustment resistance) to detect the light intensity. The photoresistor is a particular resistance that uses the photoconductive effect, and its resistance is directly related to the intensity of the incident light. When the light intensity increases, the resistance decreases; when the light intensity decreases, the resistance increases. The output signal is an analog value. The brighter the brightness, the larger the value. You can realize the function you want by judging the value of the detected light intensity, such as the function of a robot tracing light.

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2FqiGyB0EWWTrhkx3Jjeb4%2FDoubleLight_New.png?alt=media&#x26;token=6c796303-88e4-4ab0-99e4-bfefa2c8fe6d" alt=""><figcaption></figcaption></figure>

## NyBoard

### Hardware setup

Connecting to the NyBoard with a wire as shown in the following picture:

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2Fy0fNhZLXLQ9Oi6cApoW7%2FPANA0504%20%E6%8B%B7%E8%B4%9D.jpg?alt=media&#x26;token=e630ebf5-c29f-4c69-a53f-5b9a87e4d1b0" alt=""><figcaption></figcaption></figure>

### Software setup

There are two methods to upload the light mode firmware :

* Using the Petoi Desktop App
* Using the Arduino IDE

#### Petoi Desktop App

* You can use the [Firmware Uploader ](https://docs.petoi.com/desktop-app/firmware-uploader#select-the-correct-options-to-upload-the-latest-firmware)within the Petoi Desktop App.\
  Please select the correct ***Product*** type, ***Board version***, and ***Serial port*** according to your actual use. The mode should be **Light**, so press the **Upgrade the Firmware** button. \
  For example, Nybble, NyBoard\_V1\_2, COM5 as follows:\\

  <figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2Fyh1kRiGvrSDiy0GY3XsV%2Fimage.png?alt=media&#x26;token=a48869a4-9375-448f-bce0-727a49b98298" alt=""><figcaption></figcaption></figure>

#### Arduino IDE

* You can use [Arduino IDE](https://www.arduino.cc/en/software)  to upload and modify the source code.&#x20;

The code using this sensor has been integrated into the [**OpenCat**](https://github.com/PetoiCamp/OpenCat) project. Uncomment the line **`#define DOUBLE_LIGHT`**  In the **OpenCat.ino**, as shown in the figure below, use the Arduino IDE to upload the sketch to the robot main board, which can reproduce the example function of integrating the robot action.

#### Prepare the Arduino UNO development environment.

With **NyBoard V1\_\***, you can choose **Arduino Uno**.&#x20;

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2FXDvIUhP1OotHdF3rFSZN%2Fimage.png?alt=media&#x26;token=4170cb44-69cd-4d52-9701-83618dbfb59c" alt=""><figcaption></figcaption></figure>

For example,  to modify the code for ***Bittle*** as shown below:

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2FKVBVsEpugiT4vKdTIfOO%2Fimage.png?alt=media&#x26;token=5a2057a6-2cf6-469a-a707-6d82a6e75090" alt=""><figcaption></figcaption></figure>

### Arduino module test code

Suppose you want to test a light sensor's function alone or learn more about its principles. You can use the Arduino IDE to upload the test code(**testDoubleLight.ino**):

The test code is in the **OpenCat** project source code ([ModuleTests/testDoubleLight](https://github.com/PetoiCamp/OpenCat/blob/main/ModuleTests/testDoubleLight/testDoubleLight.ino))

This test code implements real-time printing of the analog values of the two analog pins (A2 and A3) in the [serial monitor](https://docs.petoi.com/arduino-ide/serial-monitor). You can also use the serial plotter to view the two analog pins (A2 and A3) more intuitively. The waveform graph is generated by the analog value of the pin output along the time axis.

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2F7GMmmKoOVYRbCSpQEE7l%2Fimage.png?alt=media&#x26;token=5ddf1c60-77f7-4e74-8d55-03fc5c7d21f1" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2FHExaZHhpKzMNB13l5wDd%2F%E5%85%89%E4%BC%A0%E6%84%9F%E5%99%A8%E6%B3%A2%E5%BD%A2%E5%9B%BE_en.png?alt=media&#x26;token=ea77ceb4-1756-4d64-b14a-8b2c00a68004" alt=""><figcaption></figcaption></figure>

## BiBoard

### Hardware setup

#### BiBoard V0&#x20;

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2FU4TxenCSpCjiSk5dvpQh%2FDoubleLight_BiBoard.png?alt=media&#x26;token=eb325db8-f7ee-463d-aac8-649e55be135f" alt=""><figcaption></figcaption></figure>

#### BiBoard V1

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2F2skkmNpA2kGV8WZX6eZE%2F%E5%85%89%E4%BC%A0%E6%84%9F%E5%99%A8%E8%BF%9E%E6%8E%A5%E5%9B%BE.png?alt=media&#x26;token=a3b61d85-9bad-438f-ba1f-30c497f7c518" alt="" width="375"><figcaption></figcaption></figure>

<figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2FkMl9brZ00GfMIbnY069j%2FLight_BiBoard_V1.jpg?alt=media&#x26;token=1bf5f3ce-998e-47fc-a63b-072368f76050" alt=""><figcaption></figcaption></figure>

For specific use, the end connected to the sensor can be fixed on the robot's head (included in Bittle's mouth or attached to the top of Nybble's head). Of course, you can also use your creativity according to your needs.

### Software setup

There are two methods to upload the firmware :

* Using the Petoi Desktop App
* Using the Arduino IDE

#### Petoi Desktop App

* You can use the [Firmware Uploader](https://docs.petoi.com/desktop-app/firmware-uploader#select-the-correct-options-to-upload-the-latest-firmware) within the Petoi Desktop App.\
  Please select the correct ***Product*** type, ***Board version***, and ***Serial port*** according to your actual use. The mode should be **Standard**, so press the **Upgrade the Firmware** button. \
  For example, Bittle, BiBoard\_V0\_2, COM5 as follows:

  <figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2FaleqWtxk5PSH9bWe9CfF%2Fimage.png?alt=media&#x26;token=c92b21ff-992f-4163-a981-86078e26eedd" alt=""><figcaption></figcaption></figure>

#### Arduino IDE

* You can use [Arduino IDE](https://www.arduino.cc/en/software) to [upload the sketch](https://docs.petoi.com/arduino-ide/upload-sketch-for-biboard#id-2.-set-up-biboard)(***OpenCatEsp32.ino***). \
  Use the latest OpenCatESP32 source code to finish the setup. For example,  to modify the code for ***Bittle*** as shown below:\\

  <figure><img src="https://1565080149-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQ6a951Q6Jn1Zzt5Ajr-887967055%2Fuploads%2FGu37FwV2ge9LKHORqrUl%2Fimage.png?alt=media&#x26;token=6b0f5ed3-eb4b-4668-8f5a-3e7462718519" alt=""><figcaption></figcaption></figure>

After uploading, there are two methods to ***activate/deactivate*** the light mode:

* Serial Monitor
  * [Open the serial monitor](https://docs.petoi.com/arduino-ide/serial-monitor#biboard) and use the serial command "***XL***" to activate the light mode.
  * Open the serial monitor and use the serial command "***Xl***" to deactivate the light mode.
* Mobile App
  * Create [a mobile app command](https://docs.petoi.com/mobile-app/controller#create-a-single-command) called "**Activate light**" and use the code: *`X76`*
  * Create a mobile app command called "**Deactivate light**" and use the code: *`X108`*
