Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

Stm32f103 microcontroller Led on/off control with Push Button as Input

By EG Projects September 4, 2021

The post explains about how to use stm32 pins as input using keil and stmcubemx ide. A simple led on/off tutorial with push button as input is made to explain the coding and working. When a push button is pressed the led turns on and when push button is released the led turns off. The project is pretty straight forward. Before beginning the tutorial i would recommend to take the tutorial on getting started with stm32f103 microcontroller, keil ide and stumcubemx code initialize. This will introduce you how to initialize/configure and align the stmcubemx and keil ide to start working with stm32f103 microcontrollers. You wont understand this project if you dint’ have introduction to stmcubemx and keil ide. 

  • Getting started with stm32f103, keil and stmcubemx

The circuit of the project is pretty straight forward. I connected the led to pin#pc-13 of stm32f103c8t6 and button to pin#pa-10. 

Stm32f103 led and button pins declaration in stmcubemx

Stm32f103 led and button pins declaration in stmcubemx

stm32f103 gpios as input/output and pull-up/pull-down resistors configuration

stm32f103 gpios as input/output and pull-up/pull-down resistors configuration
The Led pin Pc-13 is declared as output and button Pa-10 as input. Pa-10 pin as an internal pull up resistor. I enabled it and connected my button directly to ground. In the upper picture you can see that GPIO PA10 is in Input Mode and PC-13 in Output Push Pull Mode. Initial output level at PC-13 led pin is declared as high. Output frequency for Led pin set to medium. If you did not understand any thing at this level please take the tutorial mentioned above. It explains all this stuff deeply.
Stm32f103 microcontroller supports internal and external clock sources. Internally it has 2 RC oscillators HSI(High speed internal) and LSI(Low speed internal). High speed internal has 8Mhz clock and Low speed internal has 40khz clock. Externally stm32f103 supports HSE(High speed external) and LSE(Low speed external). High speed external supports max 16Mhz input clock and Low speed external supports 1000khz maximum clock. Stm32f103 has an internal PLL circuit from which we can generate clock signal of 72Mhz. We can go above 72Mhz but since stm32 works at max clock of 72Mhz so we did go above 72Mhz. In this tutorial i am using High speed internal clock source 8Mhz. Settings for HSI(High speed internal) are shown below.

stm32f103 led with push button circuit diagram

stm32f103 led with push button circuit diagram

stm32f103 clock configuration - using high speed internal clock (HSI)

stm32f103 clock configuration – using high speed internal clock (HSI)

After generating the startup initialization code from stmcubemx its time to write our desired logic “Led on/off with push button”. Its a simple code if we write it in c or c++ but in keil it all depends on the syntax supported by the compiler and the drivers/libraries we are using. I am working with Hal drivers released by the stmicroelectronics. These Hal drivers are directly installed in to our pc when we download stmcubemx software package. When we generate code from stmcubemx for keil the necessary hal drivers and libraries are directly copied to the project folder.

The while(1) loop in main() function is where we write our code. I write the below code their for our logic and its working perfectly and according to the desired output.    

        if(HAL_GPIO_ReadPin(Ibutton_GPIO_Port, Ibutton_Pin)==GPIO_PIN_RESET)  //Check if button pressed
              HAL_GPIO_WritePin(Led_GPIO_Port, Led_Pin,GPIO_PIN_SET);               //If pressed Led Switch On
        else
              HAL_GPIO_WritePin(Led_GPIO_Port, Led_Pin,GPIO_PIN_RESET);          //Else Led Switch Off

HAL_GPIO_ReadPin() function reads the status of the pin and returns the status. It needs 2 parameters to work one the port of the pin and second the pin number. In our case port is Ibutton_GPIO_Port and pin number is Ibutton_Pin.
HAL_GPIO_WritePin() writes to the specified pin. Makes it high or low. It needs 3 parameters port, pin and data. In our case port is Led_GPIO_Port pin is Led_Pin and data is SET/RESET.

  • GPIO_PIN_RESET – Write 0 to pin, Makes pin Low
  • GPIO_PIN_SET     – Write 1 to pin, Makes pin High

 

Watch the project video….

Stm32 Led and Button Code/Files

You may also like:


  • What are different types of industrial robots?

  • What are LoRa gateways and what types are available?

  • How does LoRa modulation enable long-range communication?

  • What are the different types of EV charging connectors?

  • What types of motors are used in electric vehicles?

  • What is FreeRTOS?

Filed Under: Electronic Projects, STM32

 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Reducing "shoot-through" in offline Full Bridge SMPS?
  • High Side current sensing
  • How to simulate power electronics converter in PSpice?
  • Voltage mode pushpull is a nonsense SMPS?
  • Layout IRN reduction in Comparator

RSS Electro-Tech-Online.com Discussions

  • Back to the old BASIC days
  • Parts required for a personal project
  • PIC KIT 3 not able to program dsPIC
  • Failure of polypropylene motor-run capacitors
  • Siemens large industrial PLC parts

Featured – RPi Python Programming (27 Part)

  • RPi Python Programming 21: The SIM900A AT commands
  • RPi Python Programming 22: Calls & SMS using a SIM900A GSM-GPRS modem
  • RPi Python Programming 23: Interfacing a NEO-6MV2 GPS module with Raspberry Pi
  • RPi Python Programming 24: I2C explained
  • RPi Python Programming 25 – Synchronous serial communication in Raspberry Pi using I2C protocol
  • RPi Python Programming 26 – Interfacing ADXL345 accelerometer sensor with Raspberry Pi

Recent Articles

  • What is AWS IoT Core and when should you use it?
  • AC-DC power supply extends voltage range to 800 V DC
  • Infineon’s inductive sensor integrates coil system driver, signal conditioning circuits and DSP
  • Arm Cortex-M23 MCU delivers 87.5 µA/MHz active mode
  • STMicroelectronics releases automotive amplifiers with in-play open-load detection

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe