# 3. Raspberry Pi Development Tutorial
## 3.1 Preparation
### 3.1.1 Wiring Instructions
This section illustrates connecting the ADC module to the light sensor using jumper wires. Refer to the diagram below for wiring:
> [!NOTE]
>
> * **When using Hiwonder's lithium battery, ensure that the battery connector is wired correctly: red to "+" and black to "–" on the DC interface.**
>
> * **If the connector is not attached to a lithium battery, do not connect it directly to the battery leads to avoid short circuits caused by reversed polarity.**
>
> * **Before powering on, make sure no metal objects come into contact with the controller. Otherwise, exposed pins on the underside of the board may cause a short circuit and permanently damage the board.**
### 3.1.2 Environment Configuration
Install NoMachine on the computer. The installation package can be found under:**[Appendix-> Remote Desktop Connection Tool](https://drive.google.com/drive/folders/1a6KM3GTj-OQj4Sse6sWYqs2EOpqvYYRT?usp=sharing)**.
For detailed instructions on the use of NoMachine, please refer to the corresponding directory.
> [!NOTE]
>
> **This example uses an external ADS1115 ADC module. If you use a different**
This example requires installing the library via pip. Ensure the Raspberry Pi is connected to the Internet, open a terminal, and enter the following command:
```bash
sudo pip3 install Adafruit_ADS1x15
```
After installing the library, open a terminal and navigate to the program
```bash
sudo chmod a+x Sensor_Demo/
```
## 3.2 Test Cases
This example displays the light sensor readings in the Raspberry Pi terminal and controls the module based on a configurable threshold.
### 3.2.1 Program Download
1. Open a terminal, navigate to the program directory by entering the following command, and press Enter:
```bash
cd Desktop/Sensor_Demo/
```
2. Execute the program by entering the following command:
```bash
python3 PhotoSensorDemo.py
```
### 3.2.2 Program Outcome
Use the light sensor to measure ambient light intensity. When the photodiode is covered, the sensor reading exceeds 900, above the threshold; LED1 turns off, and the terminal displays 1. When uncovered, the reading drops below the threshold, LED1 turns on, and the terminal displays 0.
### 3.2.3 Program Analysis
- **Import Libraries**
```py
import os
import sys
import time
import RPi.GPIO as GPIO
import Adafruit_ADS1x15
```
Import the required libraries for the program. The Adafruit_ADS1x15 library is specifically for the external ADC module.
> [!NOTE]
>
> **The Raspberry Pi lacks a built-in ADC, so connect an external ADC module to perform analog-to-digital conversion. This example uses the ADS1115 module, but any ADC module can be used based on specific requirements.**
- **Initialization Procedures**
```py
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
adc = Adafruit_ADS1x15.ADS1115()
adc = Adafruit_ADS1x15.ADS1015(address = 0x48, busnum = 1)
GAIN = 1
```
This step initializes the GPIO pins and configures the ADC module's A1 pin as an input.
- **Main Function**
```py
if __name__ == '__main__':
while True:
value = adc.read_adc(1,gain = GAIN)
GPIO.setup(22, GPIO.IN) # Set pin as input(设置引脚为输入模式)
state = GPIO.input(22) #Read digital value of the pin(读取引脚数字值)
print('ADC:'+ str(value) + ' | OUT:' + str(state))
time.sleep(0.5)
```
Within the while loop, store the data from the ADC module into the value variable. Configure GPIO pin 22 on the Raspberry Pi as an input and read the OUT signal from the light sensor.
Finally, print the ADC value and the OUT pin signal to the terminal.