How to interface Buzzer with STM8S microcontroller.

Problem Statement: How to interface Buzzer with STM8S microcontroller.

Material Required: 

Hardware:

  • STM8S003F3P6 Module
  • ST-LINK-V2 Programmer
  • Buzzer
  • BC547 NPN Transistor
  • 1KOhm 1/4Watt Resistor
  • Connecting Wire
Software: 
  • STVD
  • STVP
  • Cosmic CXSTM8 Special Edition 4.5.2 Compiler
Hardware Setup:

As you can see we are using a STM8S003F3P6 module for this problem. Since it is having onboard 3.3v regulator IC, which is sufficient for a buzzer to perform its operation. We should choose one pin for the output to give the signal for the buzzer. Here in the given schematic we had used D4. This D4 pin connected to the PNP transistor through a 1 kilo ohm resistor. We have connected the positive pin of the buzzer through the collector of the BC547 and negative pin to the ground. 

















Code:

 #include "stm8s.h"

 int main() {

  // Default clock is HSI/8 = 2MHz

GPIO_DeInit(GPIOD); // prepare Port B for working 

GPIO_Init (GPIOD, GPIO_PIN_4, GPIO_MODE_OUT_PP_LOW_SLOW); //Declare PB5 as push pull Output pin

TIM2_DeInit();

TIM2_TimeBaseInit(TIM2_PRESCALER_2048, 1952);

TIM2_Cmd(ENABLE);

while(TRUE)

    {

          if(TIM2_GetCounter() > 976)

          {

               GPIO_WriteHigh(GPIOD, GPIO_PIN_4);

          }

          else

          {

               GPIO_WriteLow(GPIOD, GPIO_PIN_4);

          }

    };

}




























Code Link: 

Experiment Video:













Comments

Popular posts from this blog

How to interface RGB LED with STM8S microcontroller.

How to write LED Blinking program on STM8S with timer 2 peripheral.