Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Sensor Series
      • 3D Printing
      • AI
      • ARDUINO Compatible Coding
      • Audio Electronics
      • Battery Management
      • Beginners Electronics Series
      • Brainwave
      • Digital electronics (DE)
      • Electric Vehicles
      • EMI/EMC/RFI
      • EVs
      • Hardware Filters
      • IoT tutorials
      • LoRa/LoRaWAN
      • Power Tutorials
      • Protocol
      • Python
      • RPI Python Programming
      • Sensors
      • USB
      • Thermal management
      • Verilog
      • 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
  • Guest Post Guidelines
  • Advertise
  • Subscribe

Sending a text message using SIM900 GSM with an STM32 microcontroller

By Usman ali Butt April 8, 2021

In this tutorial, we’ll learn how to send a text message using an STM32 microcontroller and SIM900 GSM module. The SIM900 can communicate with an external controller by using the universal asynchronous receiver-transmitter (UART) interface. This means that any controller with a UART module can “talk” with the module.

Fortunately, most STM32 microcontrollers have one or more UART peripherals. For this project, we’ll use the STM32f0xx discovery board with the STM32F051R8 microcontroller. The STM32’s are 3.3 volts tolerant and most SIM900s are 5 volts tolerant. There are, however, 3.3V modules available, which is what we’re using to avoid extra circuit components.

We’re also using the STM32CubeMX code configurator IDE for STM32F051R8 peripheral initialization. The STM32CubeMX offers an excellent resource where we can graphically initialize the peripherals and import the initialization code. We can, then, include our own code. 

The STM32CubeMX initialization
Open the STM32CubeMX and select the microcontroller or board (discovery, Nucleo, etc.) that you’re using for this project, ensuring you pick the correct one. If that microcontroller fails to appear in the list, you’ll need to install the package containing the particular micro.  

Double click on the microcontroller and a 3D view of it will appear on the screen. From here, you can set the individual pin functions, timers, clock speed, and other relevant settings. As we require the UART for serial communication with the GSM module, we enabled it, which makes the micro pins appear green.

For the UART parameter settings, click the UART 1 and then the parameter settings. We set the baud rate to 9600 bps, the parity to none, and the stop bit to 1. From the UART, it’s possible to take as many functions as the STM offers to include as UART features.

For this project, we’re keeping it simple and only using the UART as a basic communication protocol without advanced features.

To generate the code, press the gear icon from the menu. Give the project a name and select the tool chain IDE that you’ll work on to write and debug the code. We’re using a Keil IDE with a microcontroller development kit (MDK) ARM v5.

Afterward, click OK. The project files with your settings will be generated at the desired address.

Circuit diagram

The code
Navigate to the folder where the project files were generated. Open the “Keil” project. On the left-hand side of the project, you’ll find a file tree. If you expand the “Application/User” folder, you can access the main file. Open it.

All of the required header files and initialization functions are declared in the main file. The initialization functions are called in the main function. The header file, “stm32f0xx_hal.h,” is in the HAL libraries. These libraries are provided by STMicroelectronics. In fact, it’s possible to view all of the library functions by expanding the “Drivers/STMF0xx_HAL_Driver” folder.

The UART initialization function is below. The code is generated by the STM32CubeMX as we made all of the settings in the graphic user interface. The baud rate is 9600bps, the stop bit is 1, and the parity is none — which call all be verified in the code.  

Now, let’s jump to the main function. As you’ll note in the above code, we declared some character arrays. Each character array is holding a command for the GSM module. The SIM900 module works on the AT command set. The commands required to send an SMS are explained in the code.

To test the module, the “AT” command is sent. If the GSM replies with “OK,” then we’re good to go. However, if an “ERROR” message is received instead, then the GSM module is faulty or there might be another problem (such as with the power or network, etc.).

The HAL_UART_Transmit function from the HAL library sends the data via UART’s TX pin. It must be given the proper parameters. The first one is the UART handler structure name, which is defined in the UART library and assigned in the main file. The second parameter is the data/string. The third parameter includes the length/size of the data/string. We used the strlen() string length function to calculate the size.

The final parameter is the time from the data transmission, which we set to 10 milliseconds.   

What happens is the strcmp() string function compares the GSM’s reply with ”OK.” When this reply matches, then it enters it in the “if loop,” sending the SMS.

The GSM sends the SMS as a packet. To complete the full packet, it’s necessary to supply it with the receiver’s mobile number, followed by the text message that you’d like to send.

The end command (ctrl+Z) is extremely important as it’s what tells the GSM module to send the SMS.

Reading a received SMS is just like sending one, except for the command: AT+CMGR, AT+CMGL. However, there are several possible variations for receiving and reading, including:

  • Read the last received message.
  • Read the last received message from a particular number.
  • Read unread message (Then first second or third).

Note: typically, sim cards store up to 32 SMS’s. Any additional messages are stored in a smartphone’s memory and can be restored if requested by the user. It’s also possible that certain sim cards do no hold SMS messages.  

So, the ideal way to read and verify if an SMS is possible is to use the STM32 in debug mode. Put the “read” variables in the debug window and wait for the message output.

Where to buy the parts:

  • STM32f0 Discovery
  • GSM SIM900

  

 

 

You may also like:


  • SENDING TEXT MESSAGE USING ESP8266

  • STMicroelectronics releases STM32Cube expansion packages for smart devices

  • Build affordable computer-vision applications with STM32 microcontrollers

  • SMS-enabled scrolling message board using Arduino

  • Send Sms/text message using sim900 gsm module with Arduino Uno-Mega

Filed Under: Microcontroller Projects
Tagged With: STMicroelectronics
 

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.

Submit a Guest Post

submit a guest post

EE TECH TOOLBOX

“ee
Tech Toolbox: Power Efficiency
Discover proven strategies for power conversion, wide bandgap devices, and motor control — balancing performance, cost, and sustainability across industrial, automotive, and IoT systems.

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.

  • Methods for Calculating SNR and SFDR in a CUI for Use in Machine Learning
  • rechargeable battery and simple alkaline battery in one single product
  • Industrial Network Market Analysis and Introduction to Common Bus Protocols
  • Ensuring Reliable Backups in Azure
  • Op-Amp oscillating

RSS Electro-Tech-Online.com Discussions

  • WTB: "The Theory Of Servicing AM, FM, And FM Receivers" by Clarence R. Green and Robert M. Bourque
  • Anyone In The US Ordered From AliExpress Recently?
  • Calculation of A Class amplifier
  • strange laptop problem
  • restarting this Christmas project

Featured Tutorials

Real Time Hardware Filter Design

  • Practical implementation of bandpass and band reject filters
    Practical implementation of bandpass and band reject filters
  • Practical application of hardware filters with real-life examples
    Practical application of hardware filters with real-life examples
  • A filter design example
    A filter design example
  • Types of filter responses
    Types of filter responses
  • What are the two types of hardware filters?
    What are the two types of hardware filters?
  • What are hardware filters and their types?
    What are hardware filters and their types?
More Tutorials >

Recent Articles

  • Stackpole introduces compact jumpers for high-current circuit routing
  • Ironwood Electronics launches near-device-footprint SMT elastomer socket for BGA264
  • Amphenol RF releases P67 FAKRA plugs for 6 GHz RF transmission
  • Microchip releases platform to deliver real-time specifications for AI assistants
  • GigaDevices introduces 32-bit MCUs with integrated DSP and FPU support

EE ENGINEERING TRAINING DAYS

engineering
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • 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
      • Sensor Series
      • 3D Printing
      • AI
      • ARDUINO Compatible Coding
      • Audio Electronics
      • Battery Management
      • Beginners Electronics Series
      • Brainwave
      • Digital electronics (DE)
      • Electric Vehicles
      • EMI/EMC/RFI
      • EVs
      • Hardware Filters
      • IoT tutorials
      • LoRa/LoRaWAN
      • Power Tutorials
      • Protocol
      • Python
      • RPI Python Programming
      • Sensors
      • USB
      • Thermal management
      • Verilog
      • 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
  • Guest Post Guidelines
  • Advertise
  • Subscribe