The OLED displays the light sensitivity value

This tutorial demonstrates how a 0.91 inch OLED driven by a hardware I2C displays the converted values of a photosensor ADC.

1、software-hardware

2、Brief principle

2.1、Hardware schematic diagram

The schematic only shows the I2C (I2C1) and photosensor interfaces used in the tutorial

image-20231025115535433

2.2、Physical connection diagram

image-20231025115633214

OLEDSTM32 board
VCCVCC
SCLSCL
SDASDA
GNDGND

2.3、Principle of control

The use of I2C1 peripheral interface and 0.91 inch OLED display for communication, I2C related knowledge will not be introduced, you can see before [3, development board basic tutorial: I2C Communication]

By using an ADC (analog-to-digital converter), the analog voltage output by the photosensor can be converted to a digital value.

The STM32F103ZET6 contains three 12-bit analog-to-digital converters, and the converted value range is 0 to 2^12-1 (i.e., 0 to 4095).

Photosensitive sensor (development board integrated)Corresponding pin
LIGHT (analog signal output)PF8(ADC3_IN6)
product0.91 inch OLED screen
resolution128*32
Control chipSSD1306
Means of communicationI2C(IIC)
Voltage of operation3.3~5V
I2C interfaceVCC:Power supply positive(3.3/5V)
GND:Power supply ground
SCL:I2C bus clock signal
SDA:I2C bus data signal

3、Engineering configuration

Project Configuration: Prompts for configuration options in the STM32CubeIDE project configuration process

3.1、Notes

Omitted project configuration: New project, chip selection, project configuration, SYS for pin configuration, RCC configuration, clock configuration, and project configuration content

The project configuration part, which is not omitted, is the key point to configure in this tutorial.

3.2、Pin configuration

image-20231025142419063

image-20231025142535373

image-20231025142728769

image-20231025142800796

image-20231024193047747

4、Main Function

This part mainly introduces the functional code written by users. Detailed code can be opened by yourself in the project file we provide, and enter the Bsp folder to view the source code. .

User function

Many of the common HAL library functions were covered in Chapter 3, but they will not be covered here.

function:user_lightseek_detect

Function prototypesvoid user_lightseek_detect(void)
Functional DescriptionOLED displays the resistance of the light sensitivity
Input parametersNone
Return valueNone

function:Adc_Get_seek_finally

Function prototypesuint32_t Adc_Get_seek_finally(void)
Functional DescriptionThe photosensitive voltage is converted to resistance
Input parametersNone
Return valueValue of resistance

function:Adc_Get_seek_finally

Function prototypesuint16_t Adc_Get_seek(uint32_t ch)
Functional DescriptionThe photosensitive voltage was collected
Input parametersADC channel
Return valueValue of voltage

5、Experimental phenomenon

After downloading the program successfully, press the RESET button of the development board to observe the OLED display

Phenomenon::

OLED display The first line shows oled init success! (boot display once), the third row shows the photosensitive sensor resistance value.