In this project, we will interface the STM32F103C8 microcontroller with the MCP3008 analog-to-digital converter (ADC) using the Serial Peripheral Interface (SPI) protocol and display the results on a character LCD.
The MCP3008 is a 10-bit ADC with 8 input channels, making it ideal for reading multiple analog sensors such as LDRs, temperature sensors, and potentiometers. It converts analog signals into 1024 discrete digital values and operates on a 2.7V–5.5V supply, ensuring compatibility with STM32.
STM32CubeMX Setup- Create a new project for STM32F103C8.
- Go to Clock Configuration → set system clock to 72 MHz.
- Configure SPI1 as Full-Duplex Master.
- Clock Polarity
= Low
- Clock Phase
= 1 Edge
- Configure GPIO Pins:
- PA8
→ Output (Chip Select)
- PB5–PB11
→ Outputs (LCD)
- Generate initialization code.
- Create driver files:
- MCP3008.h , MCP3008.c
- LiquidCrystal.h
, LiquidCrystal.c
- Implement core
MCP3008
functions:
- void MCP3008_Init(MCP3008_SPI* spi, SPI_HandleTypeDef* hspi, GPIO_TypeDef* CS_PORT, uint16_t CS_PIN);
- uint16_t MCP3008_Read_Channel(MCP3008_SPI* spi, uint8_t channel);
- Implement LCD control functions:
- void noDisplay(void);
- void display(void);
- void blink(void);
- In
main.c
, add application logic:
- Initialize SPI and LCD
- Read ADC values from LDRs via MCP3008
- Display values on LCD
- Add components:
- STM32F103C8
- MCP3008 (ADC)
- 16x2 LCD (4-bit mode)
- LDRs and DC voltmeters for analog inputs
- Connect wiring:
- SPI1 ↔ MCP3008 (PA5–PA7, PA8 for CS)
- LCD ↔ STM32 (PB5–PB11)
- Load the generated .hex firmware into the STM32.
- Run the simulation and verify that LDR readings are displayed on the LCD.
If you have any questions or suggestions don't hesitate to leave a comment below
Comments