ESP32 Interrupts
Hello readers, hope you all are doing great. Today, we will discuss interrupts and timers in ESP32 and how to handle internal as well as external interrupts. So, we will discuss What is interrupt, Polling, ESP32 interrupt, Software interrupts, Hardware Interrupts, IRS (Interrupt Service routine), Steps to execute an interrupt or how is an interrupt handled in the microcontroller, Code description for ESP32 interrupts with Arduino IDE, Code description for hardware interrupts, Why is it preferred to use timer to add delay instead of using delay() function. So, let's get started:
| Where To Buy? |
|---|
| No. | Components | Distributor | Link To Buy |
| 1 | ESP32 | Amazon | Buy Now |
What is Interrupt?
- Interrupts are used when a micro-controller needs to continuously monitor for an event while the same micro-controller is executing a particular task.
Fig 1 Interrupt
- Each interrupts has a priority level and each interrupt is executed as per their priority level.
- You can mask or unmask a particular interrupt depending upon their properties and your requirements.
Polling
Polling is a process that performs continuous monitoring. Basically, the processor continuously monitors the state of a specific device or a peripheral, and when the status of the device satisfies the condition, the device executes the task that was required. Then it moves on to the next device to monitor until each one has been served. The processor performs no other operations and devotes all of its processing time to monitoring, and all other tasks are suspended until the current one is completed.
Fig 2 polling vs Interrupt
So, to overcome the disadvantage of the polling method, we chose the Interrupt method.
ESP32 Interrupt
ESP32 module has a dual-core processor and each core consists of 32 interrupts. Basically interrupts are of two types:
Software Interrupts:
Fig 3 ESP32 software interrupt
Software interrupts are internal which occur in response to the execution of a software instruction. For example, a timer can be used to generate a software interrupt.
Hardware Interrupts:
Fig 4 ESP32 software interrupt
Hardware interrupts are the external interrupts that are caused by an external event. For example, an external push button connected to ESP32’s GPIO or a motion sensor will generate an interrupt (event) if a motion is detected.
ISR (Interrupt Service routine)
When an interrupt occurs during normal program execution, an ISR (interrupt service routine) or an interrupt handler is called into action. The normal program execution will be halted, and the interrupt will be executed based on the priority level of the interrupt.
Fig. 5 Interrupt service routing
Every interrupt has a fixed memory location where the address of the ISR is stored.
Interrupt Vector Table refers to a memory table or memory table that is used to store the location of an interrupt service routine.
Note: IRAM_ATTR attribute should be defined for interrupt handling. As per the ESP32 datasheet interrupt service routine should run inside the RAM. Because inside the RAM it is fast to execute a code than in flash memory and when an interrupt occurs all the other tasks will be blocked or halted till the time interrupt request is served.
Steps to execute an interrupt in ESP32
When an interrupt occurs, the microcontroller will go through the following steps:
- The microcontroller will halt the current task and will store the address of the next instruction (Program Counter or PC) on the stack (lower byte first).
- The microcontroller will execute the higher priority interrupt first and will block the lower priority interrupts.
- It jumps to the interrupt vector table memory location that contains the address of the interrupt service routine (ISR).
- The microcontroller reads the interrupt vector table and jumps to the address of the ISR. It begins executing the interrupt service subroutine.
- The microcontroller returns to the location where it was interrupted after executing the RETI instruction. First, it obtains the program counter (PC) address from the stack by inserting the stack's top bytes into the PC. The execution will then begin at that address.
Fig. 6 ESP32 Interrupt Program flow
ESP32 Interrupt Code
We are using Arduino IDE to compile the code and then upload into the ESP32 board.
If you are not familiar with the procedure of getting started with Arduino IDE and hoe to compile a code in Arduino IDE then follow our previous tutorial that is Introduction to ESP32 programming series.
// Set GPIOs for LED and Push button
const int led = 2;
const int button = 0;
// Timer: Auxiliary variables
#define timeSeconds 10
unsigned long now = millis();
unsigned long lastTrigger = 0;
boolean startTimer = false;
// Checks if button input was detected, sets LED HIGH and starts a timer
void IRAM_ATTR buttonInput() {
Serial.println("input is available !!!");
digitalWrite(led, HIGH);
startTimer = true;
lastTrigger = millis();
}
void setup() {
// Serial port for debugging purposes
Serial.begin(115200);
pinMode(button, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(button), buttonInput, RISING);
// Set LED to LOW
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
}
void loop() {
now = millis();
// Turn off the LED after the number of seconds defined in the timeSeconds variable
if(startTimer && (now - lastTrigger > (timeSeconds*500))) {
digitalWrite(led, LOW);
startTimer = false;
}
}
Code for ESP32 Interrupts with Arduino IDE
As we have already discussed that the interrupt could be software generated (internal interrupt) or due to hardware (external interrupt). This tutorial we are using hardware or external interrupt for demonstration. You can also use software interrupts or both in a single code, as per your requirements.
Code Description for Hardware interrupt
- For the demonstration of hardware interrupt we are using a push button which is connected to the interrupt pin and a LED for the show output.
- First of all, let us assign two GPIOs, one is for push button (input) and another is LED. We are using the built-in LED that is GPIO_2 and a built-in push button (named as BOOT on ESP32 board) connected to GPIO_0.
Fig. 7
In this code, we are using timer to add delay instead of using delay() function.(I will also explain that why it is preferred to use timer in order to create delay instead of using delay() function after the code demonstration).
- Next, we are defining variables which are used to set the timer for adding delay after the interrupt is being detected.
- The variable now is defining the current time
- The variable lastTrigger is defining the time when the interrupt is detected.
- The variable startTimer is used to start the time when an interrupt is detected.
IRAM_ATTR
- It is required that the interrupt service routine should have the minimum possible execution time because it halts or blocks the normal program execution.
- The attribute IRAM_ATTR is used to run the code (interrupt code) inside the internal RAM when an interrupt occurs because RAM (random access memory) is much faster than flash memory.
- After the execution of the interrupt code or ISR the normal code will be stored or executed inside the flash memory.
Arduino Setup() Function
- Inside the setup() function we are initializing the serial communication with a baud rate of 115200.
- Set the push button GPIO pin a pulled up input pin.
- attachInterrupt() function is used to set the button pin (GPIO_0) as an interrupt pin and it will button input (interrupt) during the falling edge.
- A function called detachInterrupt() can be used if you no longer want to use a GPIO pin as an interrupt.
- Change the state of the LED when an interrupt is detected for
Arduino Loop() Function
Inside the loop function which is continuously running, the buttonInput function will be called every time when an interrupt occurs , which we have defined previously inside the setup() function.
- LED will turned off after the delay of 5sec once an interrupt is detected.
- The variable “now” will be updated every time with the current time.
- We can check the interrupt details on serial monitor as shown in the image below:
Fig 14 Serial monitor
Why it is preferred to use Timer instead of delay()?
Delay() function is a complete software process and it is mostly used because it is easier to implement delay using only software. On the other hand, when we switch to hardware delay or use a timer to add delay the process is a bit complicated to implement.
But, when we think of a practical perspective we prefer hardware delay over software delay. Because a software delay keeps the processor busy in a continuous loop and the processor need to keep all other tasks on halt.
On the other hand, if we use a timer to add delay the processor can complete some other task while the timer is playing its own part.
This concludes the tutorial. Hope you find it helpful. In our next tutorial, we will discuss the ESP32 Web Socket server.
ESP32 Web Server in Access Point (AP) Mode
Hello readers, hope you all are doing great. This is our 3rd tutorial in the ESP32 programming series. In our previous tutorial, we discussed the ESP32 Web server, where we created the ESP32 web server in STA mode.
ESP32 can be operated as an access point (AP) or a Wi-Fi station (STA mode). So, in this tutorial, we will create an ESP32 web server in access point (AP) mode. Here's the video demonstration of ESP32 WebServer in Access Point Mode:
As I mentioned above, in our 2nd tutorial, we already discussed the basics of the ESP32 web server. So, in this tutorial, we will only discuss how to create the ESP32 in access point mode.
For detailed information about the basics of the ESP32 web server and how client-server communication takes place, follow our previous tutorial (i.e., Create a Web Server with ESP32).
| Where To Buy? |
|---|
| No. | Components | Distributor | Link To Buy |
| 1 | ESP32 | Amazon | Buy Now |
What happens in Access Point (AP) mode?
In Access Point Mode the ESP32 creates its own wireless Wi-Fi network in this mode, similar to the one provided by your existing router. In access point mode, we don't need to connect the ESP2 to a Wi-Fi network. In the Wi-Fi network it creates, the ESP32 Wi-Fi board can connect up to 5 devices.
Fig 1 ESP32 as an Access Point
So, in access point mode, nearby Wi-Fi devices such as mobile phones, laptops, or a secondary ESP32 module acting as a station can connect directly to the AP (ESP32 module) without the need for an external Wi-Fi router.
On the other hand, in Station mode, the ESP32 wi-fi module connects to your Wi-Fi network through a router. The router acts as a conduit for communication between the web client and the ESP32. The Wi-Fi router provides the IP address. This IP address can be used by web clients to connect to the Web server on a local network.
To know about how to set up/operate Arduino IDE for ESP32 compilation, follow our first tutorial i.e., Introduction to ESP32 programming series.
ESP32 Web Server in Access Point (AP) Mode
Here we are using an inbuilt example from Arduino IDE(ESP32). You can modify the example code as per your requirements or can write your own code.
- To find the Wi-Fi Access Point example in Arduino IDE :
- Click on File from the top menu bar.
- Place the mouse cursor on the example option from the list.
- Look for the WiFi option.
- There you will find the WiFiAccessPoint option, click on that and compile the program.
A screenshot is attached below to help you find the example code in Arduino IDE.
Fig 2 Wi-Fi access point example
The first task while writing the WiFi code is to add the required wifi header files or libraries in the code.
Here we are adding three libraries.
- WiFi.h: This header file contains all the functions related to Wi-Fi activities like enabling the Wi_Fi, connecting to a wi-fi network etc.
- WiFiClient.h: This header file is used to create a client that can connect with a specific IP address.
- WiFiAP.h: This header file is used to configure and manage ESP32’s wifi module in Access Point (AP) mode.
Fig 3: Libraries
Define the LED pin or a GPIO (for peripheral interface) which we going to control through web server. Here we are using the inbuilt LED which is internally connected with GPIO2
Give a name (SSID) to the ESP32 Access Point and set the password for security purpose ( if you wish to).
While creating a web server we also need to assign a port and usually port 80 is used for local web server.
Arduino Setup() function
Inside the setup function, the LED pin is initialized as an output one and then initialized the serial monitor with a baud rate of 115200.
The next task is to configure the ESP32 Wi-Fi module in access point mode. For that, here we are calling a function called WiFi.softAP. Where we are passing two parameters, ssid and password, respectively.
After configuring the AP mode, we need to fetch the IP address of the access point by calling the WiFi.softAPIP() function and printing it on the serial monitor.
Then, after fetching the IP address, we will start the server using the server. perform.
Arduino Loop() function
After configuring the Access Point mode and initializing the server, the server will next wait for the station or client connection, which can be a mobile phone, a laptop, or another ESP32 board configured in STA mode.
Once the connection is established between the access point and the client device, the access point will wait for the data input.
A string type variable called currentLine has been defined to hold the incoming data from the client.
If there is a byte to be read from the client, then it will be stored inside the char type variable c.
HTTP header always starts with a response code e.g.: HTTP/1.1 200 ok
An HTML page will be created on the client’s browser, from where the client device can control (ON/OFF) the LED.
Different URLs will be created to turn ON and OFF the LED depending upon the HTML input received from the client device i.e., H (to turn ON the LED) and L ( to turn OFF the LED).
Client.stop() function is responsible for closing the connection between Access Point and client or station device.
Note: If you need any guidance regarding how to upload or compile a code for the ESP32 module in Arduino IDE, follow our first tutorial on the ESP32 programming series.
Testing ESP32 web server with hardware in Access Point with Arduino IDE
Here we are going to control the ESP32’s inbuilt LED through an ESP32 web server (AP mode).
We will connect our station or client device through Wi-Fi to the ESP32 module, which (ESP32) is currently acting as an access point (AP).
To establish the connection go to your mobile phone’s Wi-Fi setting.
The Access Point is advertising itself with a pre-defined SSID so that the station devices or clients can find the AP device and can communicate with each other.
If you find a wi-fi device (AP) named ESP32_AP (or as per your SSID) connect to that after entering the assigned password.
Fig. Scanning for available Wi-Fi devices in mobile phone
Fig. Connected with ESP32 AP
As we are using the inbuilt LED, no external components are required.
After connecting to the access point, you can find the IP address of the AP device printed on the Serial Monitor. As shown in the image below:
Fig.: Serial Monitor
Enter the IP address in the browser. Now you can turn the LED ON or OFF using the web page as shown in the images below.
A web page with URL 192.168.4.1/H will be displayed on the browser when LED is turned ON
Fig.: URL when LED is turned ON
LED is blue color represents the inbuilt LED which is connected to GPIO_2.
Fig.: ESP32 LED ON
Another web page with URL 192.168.4.1/L will be created when the AP will receive the input to turn OFF the inbuilt LED. As shown in the image below:
Fig.: Web page displaying the LED off state.
This concludes today’s tutorial. We hope you find it helpful.
In our next tutorial, we will discuss another ESP32 feature that is BLE (Bluetooth low energy).
ESP32 Bluetooth Classic in Arduino IDE
Hello readers, I hope you all are doing well. Welcome to the Section 2 (ESP32 Features) of the ESP32 Programming Series. ESP32 is equipped with numerous built-in features and in each chapter of this Section 2, we will explore one of these ESP32 features in detail.
In the previous Section(Section 1: ESP32 IDEs), we installed different software IDEs to program ESP32 boards. Among these IDEs, we are going to use Arduino IDE for programming ESP32. So, I hope all of your tools are configured properly and you are ready to explore the built-in features of ESP32.
Today's the 1st Chapter of Section 2, and here we will discuss How to communicate with ESP32 Bluetooth Classic from a smartphone using Arduino IDE.
Here's the video tutorial for ESP32 Bluetooth Classic:
| Where To Buy? |
|---|
| No. | Components | Distributor | Link To Buy |
| 1 | ESP32 | Amazon | Buy Now |
ESP32 Wireless Features
ESP32 is equipped with 3 wireless communication protocols:
- Bluetooth Classic
- Bluetooth Low Energy(BLE)
- Wi-Fi
Before going forward, let's first have a look at the basic features of BT Classic:
What is Bluetooth Classic?
Bluetooth is a short-range communication(wireless) technology, used in electronic devices(i.e. mobile phones, computers, LED, headphones, speakers etc.) for wireless communication over a short distance, approximately 15m. Bluetooth operates at a 2.4GHz ISM band. Bluetooth uses low-energy radio waves for data communication between Bluetooth-enabled devices.
Now, let's design the code to communicate over ESP32 Classic BT:
ESP32 Bluetooth Classic
We are using Arduino IDE for code compiling and uploading to the ESP32 module. I hope you have already installed ESP32 Boards in Arduino IDE. So, let's design a simple project to understand the working of ESP32 Bluetooth Classic:
Project Description
First of all, we will install a "Serial BluetoothTerminal" App from the Google Play Store to communicate with the ESP32 Classic BT.
In this project, we will first enable the ESP32 Classic Bluetooth, so that we can connect it to our smartphone. After a successful connection, we will send data from our smartphone(Serial Bluetooth Terminal App) to the ESP32 Serial Terminal and vice versa.
So, let's first understand the ESP32 BT Code and then will install the Serial Bluetooth App from the Google Play Store:
Code for ESP32 Classic BT
- Open Arduino IDE and navigate to "File > Examples > BluetoothSerial > SerialtoSerialBT".
- This code utilizes BluetoothSerial Library, it's pre-installed with Arduino IDE but if you can't find it in the Examples, you can manually Download Bluetooth Serial Library and add it from Library Manager in Arduino IDE.
- Upload this code to your ESP32 Microcontroller Board.
Here's the complete code:
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to enable it
#endif
BluetoothSerial SerialBT;
void setup() {
Serial.begin(115200);
SerialBT.begin("TEP_ESP32_BT"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}
void loop() {
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(20);
}
Let's understand the code working:
How the Code Works
- First of all, we added the Classic Bluetooth Library named "BluetoothSerial", it has all the routines/functions required to enable Bluetooth and to communicate with other devices.
#include "BluetoothSerial.h"
- Next, we placed a check to ensure that Classic Bluetooth is configured properly and is discoverable to other devices:
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to enable it
#endif
- Next, we created a Bluetooth object "SerialBT" of class BluetoothSerial to initialize the Bluetooth stack and communicate serially with ESP32 Classic Bluetooth:
BluetoothSerial SerialBT;
Setup() Function
Initial Configurations of the project are added in the Setup() function. In our code:
- First, we initialized the Serial Port at a baud rate of 115200.
- Next, we initialized the SerialBT object and assigned a unique name "TEP_ES32_BT" to our Bluetooth device, this name will appear in the Bluetooth Search List.
- Finally, printed a welcome message on the Serial Monitor.
void setup() {
Serial.begin(115200);
SerialBT.begin("TEP_ESP32_BT"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}
Loop() Function
The Loop() Function is an infinite loop and is equivalent to while(1) in normal C Language. In our code, we have placed two if checks:
- The first "IF Check" is monitoring the ESP32 Serial Terminal.
If we send any data from the Serial Terminal, this data will be transmitted to the SerialBT.
- The second "IF Check" is monitoring the SerialBT.
If we receive any data via ESP32 Classic Bluetooth, we will print it on the Serial Terminal.
void loop() {
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(20);
}
So, I hope you have understood the working of this ESP32 Classic Bluetooth code. Now, let's install the Serial Bluetooth Terminal App from the Google Play Store:
Serial Bluetooth Terminal App
- Make sure your mobile's Bluetooth is enabled.
- Open the Google Play Store on your Smartphone and make a search for "Serial Bluetooth Terminal" and install it.
If we are connecting with the ESP32 BT for the first time, we need to pair it first.
- Open the Serial Bluetooth Terminal app and click on the "Devices" tab.
It will scan the list of all the available Bluetooth devices:
[Image]
- Now, Pair with the ESP32 Classic BT device named "TEP_ESP32_BT".
- Click on Pair.
We have successfully paired the ESP32 BT with the smartphone's Bluetooth.
ESP32 BT to Smartphone - Data Testing
- Open the Bluetooth Terminal App and click on the Connect Button at the top:
[Image]
- Open the Serial Monitor in the Arduino IDE and set the baud rate to 115200:
[Image]
- As shown in the below figure, when we send data from the Serial Monitor, it communicates over Classic Bluetooth and appears in the BT Terminal App.
- Similarly, when we send data from the BT Terminal App, it appears on the Serial Monitor of Arduino IDE.
So, that's how we can communicate between ESP32 and smartphones over Classic Bluetooth. In today's lecture, we communicated simple text data to understand the working principle. In the upcoming lectures, we will send complex data(i.e. commands & sensor values) via Classic Bluetooth.
Now, let's have a look at some theoretical knowledge about Classic Bluetooth:
BLE vs Bluetooth Classic
Fig: BLE vs Classic Bluetooth
- Bandwidth: Bluetooth can send a large amount of data, while BLE sends small chunks of data.
- Compatibility: Classic Bluetooth and BLE are not compatible with each other. A Bluetooth-supported device can’t communicate with BLE supported device.
But, a device having BT V4 (Bluetooth version 4) can discover both BLE and Classic Bluetooth devices.
- Power consumption: The classic Bluetooth consumes more power than BLE.
- Pairing: In Bluetooth classic pairing is necessary before sharing data between Bluetooth devices for security purposes. On the other hand, BLE technology doesn't ask for pairing before data transmission.
- Number of active devices: In traditional Bluetooth, a maximum of 7 slave devices can be connected with the master Bluetooth at a time. Though classic Bluetooth can connect with multiple nodes/slave devices at a time but it can exchange data with only a single node at a time.
Bluetooth Evolution
- The initial Bluetooth version (V1.0) was riddled with bugs and limitations.
- Bluetooth 2.0 was created as a result of various modifications and improvements to the basic version 1.0.
- Bluetooth 2.0's most notable feature was the enhanced data rate (EDR).
- Fast modulation technology and a data rate of up to 3Mbps are used in Enhanced Data Rate mode.
- Despite improvements in the basic version, Bluetooth 2.0 lacks a security feature.
- Bluetooth 2.1 added a security feature called "Pairing" as well as a faster data rate.
- Another updated version, Bluetooth 3.0, included a Wi-Fi feature, but it was rarely used, and when it was, the features were similar to the Bluetooth 2.1 version.
- Bluetooth 4.0 was the first version to include the Bluetooth low energy feature (BLE).
- The most recent Bluetooth version is v5.2, which supports both Classic Bluetooth and BLE and consists of the following features:
- EATT (enhanced attribute protocol)
- LE (Low Energy) power control feature (LEPCF)
- LE Audio
Bluetooth Network topology
- Classic Bluetooth forms a piconet. A piconet has a single master and multiple(max 7) slaves. Each piconet has its own hopping sequence.
Fig: Classic Bluetooth Network topology
Classic Bluetooth can operate on both point-to-point and point-to-multi-point network topology. In traditional Bluetooth, a maximum of 7 slave devices can be connected with the master Bluetooth at a time. Though, classic Bluetooth can connect with multiple nodes/slave devices at a time, but it can exchange data with only a single node at a time.
Bluetooth Clock
In classic Bluetooth, the piconets are not synchronized.
The clock is one of the most important aspects of Bluetooth. In a Bluetooth connection, the master device has a clock that is used to split the time on each physical channel. Clocks on all slaves in a connection are synchronized to the master clock.
Bluetooth clock synchronization is essential because the radios must agree on when to transmit. Because Bluetooth uses precise timeslots for transmissions with devices alternating, if the clocks are not synchronized, there may be issues with devices transmitting at the incorrect time.
Classic Bluetooth transmitting power
It is defined in multiple classes:
- Class 1: +20dBm maximum.
- Class 2: Up to +4dBm.
- Class 3: Up to +0dBm.
Classic Bluetooth Data transmission modes
Generally, there are two data transmission modes:
- Basic Rate (BR): BR is the first Bluetooth protocol which is implemented in Bluetooth v1.0. It uses one of the FSK (frequency shift keying) modulation techniques known as Gaussian frequency-shift keying (GFSK) and communicates data at the 2.4 GHz ISM band.
- Enhanced Data Rate (EDR): It's a Bluetooth specification that allows for a higher data rate or speed. It is not available in all Bluetooth versions, and its availability is dependent on the Bluetooth version and profile. EDR uses pi/4-DQPSK (differential quadrature phase-shift keying) and 8DPSK (differential phase-shift keying) modulation techniques with data rates of 2Mbps and 3Mbps respectively.
Bluetooth packet format
- When two devices communicate data over Classic Bluetooth, they use SPP (Serial Port Profile)
Fig. Bluetooth packet format
Enhanced data rate packet sends the Access code and header using the basic rate and this process uses GFSK (Gaussian Frequency Shift Keying). The guard gives the time to change the modulation to EDR modulation and then the synch word (64 bits), payload, and Trailer (4 bits) bits are sent using EDR (enhanced data rate) modulation.
So, that was all for today. In the next lecture, we will communicate between ESP32 and smartphones via BLE(Bluetooth Low Energy). Till then take care. Have a good day!!!
Smart Coffee Vending Machine using Arduino
Hello geeks, Welcome to our new project. As most readers have already seen the coffee vending machine or maybe you are drinking coffee while reading this article and if you are a tinker or a geek, it must have come to your mind how to make a coffee vending machine on your own. In today's tutorial, we are going to learn how to make a Smart Coffee Vending Machine using Arduino with Proteus Simulation for the same.
We can use this project for an engineering project’s showcase for electronics, electrical engineering students, and can be used in offices as well.
Coffee is the second most popular drink in the world and it is one of the oldest beverages of the world. According to Wikipedia, more than 2 billion cups of coffee are consumed every day in the whole world. As engineers or working professionals, we all know how coffee is very important for us. Having a good coffee makes our day better and refreshes the mood. Research shows coffee drinkers tend to live longer but when keeping it in moderate consumption. And making a good coffee is one of the most skillful jobs and time-consuming processes as we want our coffee in minutes. Now here our project comes to the picture, this smart coffee vending machine can make a good coffee in a couple of minutes. There are various flavors of coffee and our smart coffee vending machine can provide us with 4 different flavors which are the most commonly loved such as Latte, Cappuccino, Espresso, and Cafe Mocha. Here's the video demonstration of this project:
| Where To Buy? |
|---|
| No. | Components | Distributor | Link To Buy |
| 1 | DC Motor | Amazon | Buy Now |
| 2 | LCD 20x4 | Amazon | Buy Now |
| 3 | Arduino Uno | Amazon | Buy Now |
Software to Install:
As we are going to design this project using Proteus Simulation, instead of using real components. As in the simulation, we can figure out the issue which may occur while working on real components and that can damage our components.
Proteus is the software for simulation and designing electronics circuits. As Proteus software has a big database of electronics components but still it does not have few modules in it like Arduino boards or LCD modules etc.
So we have to install the libraries, which we are going to use in this project:
- Arduino Library for Proteus: We have to add the Arduino boards to the Proteus components list.
- LCD Library for Proteus: We have to add the LCD module to Proteus Suite.
You can download this whole project for example Proteus Simulation and Arduino Code, by tapping the below button
Smart Coffee Vending Machine using Arduino
These are required components for Smart Coffee Vending Machine, as follows:
- 20X4 LCD display: It is used to display user-related messages like the state of the vending machine.
- Arduino UNO: It is used as the brain of our project. All operations and decision-making will be done using this microcontroller.
- DC motor: It is used for dispensing the ingredients of coffee and the mixer.
- Buttons: It is used as a user interaction option.
As a suggestion, whenever we make a project, it should be like a product, as it should be user friendly and interactive, so considering that we have used an LCD module to display the messages related to available coffee flavors and their individual prices so that users can easily select them using buttons and DC motors to pour the ingredients related to coffee like water, sugar, coffee powder, and milk, and a mixer for blending the coffee.
We have connected the LCD using an I2C GPIO expander as we have limited GPIO pins to connect other peripherals with Arduino UNO. I2C Gpio expander requires only two pins as we know that I2C uses SCL(Serial Clock) and SDA(Serial Data) pins for communication.
Components Needed:
- Arduino UNO
- LCD display
- 4 Buttons
- 8 Motors
- PCF8574
Components Details
Arduino UNO:
We can use any Arduino development board but here in this project, we have used an Arduino UNO board.
- Arduino UNO is one of the programmable, open-source microcontroller boards of the Arduino family.
- It contains an Atmel’s Microchip ATMega328 or ATMega328P microcontroller which has Harvard architecture 8-bit RISC processor core and 32 KB flash memory.
- Arduino UNO comprises 14 digital I/O pins out of which 6 are PWM pins as well and 6 Analog I/O pins with 10 bits resolution(0-1024).
- Arduino UNO has only 1 hardware UART pin(but we can use other pins also for UART communication using SoftwareSerial library in Arduino), 1 I2C, and 1 SPI.
PCF8574:
We have used this IC as a GPIO expander for our project as we have restrictions on the availability of GPIO pins in Arduino UNO.
- It is an 8-bit I/O, silicon-based CMOS GPIO expander.
- It can be used to write data on the pins and also can read data on those pins.
- It uses the I2C protocol for communication with the master device.
- As we know that I2C protocol uses the slave address to send or receive data from slaves, so for that it has 3 pins A0, A1, A2 for setting the slave address.
- Slave address for PCF8574 starts from 0x20 to 0x27. That means we can add only 8 PCF8574 IC directly to a master controller.
- The following image explains the logic of the slave address of PCF8574.
- It is used for connection for the LCD module with Arduino UNO in our project.
- If you want to learn more about IC PCF8574, you can refer to the datasheet using the following URL: PCF8574 Datasheet
LCD display
The LCD display is used to show the user-related messages in this project.
- LCD is a short form of Liquid Crystal Display which is basically built using Liquid Crystal technology.
- There are different sizes of LCDs available, in this project we have used 20X4 size.
- Here 20X4 signifies that it can display 80 ASCII characters at a time.
- There are 16 pins in the LCD. We will not use every pin of LCD in this project.
- It has 8 data pins, 1 Read/ Write select pin, 1 Register mode pin, 1 Enable pin, 2 pins for backlight, and 2 pins for power supply, 1 contrast control pin.
- There are mainly two types of register in the LCD: Command Register and Data Register.
- When we set the RS(Register Select) pin to logic High then it will select the data register mode and in logic Low, it will select the command register.
- To display the data on LCD we will set the RS pin to logic High.
Proteus Simulation of Smart Coffee Vending Machine :
Now, it's time to start designing the Proteus Simulation of our Smart Coffee Vending Machine.
- Most importantly, ensure that Proteus is installed on your PC and download all the required libraries for Proteus ahead.
- For this project, we are going to need libraries of Arduino and LCD modules.
- Make sure that you have read about how to use libraries in Proteus software.
Let’s create a new project, open the new project in Proteus and import all the required components which we are going to use, and place them within the working area.
- We need the following components, so select all of them from the Proteus component library.
Circuit Diagram and Working:
- Now let’s design our circuit, first place all the selected components in the Proteus Workplace, as shown in the image below:
- We will start connecting the LCD module and PCF8574, as we are using only 4-data pin-mode of LCD.
- After that, we will start the GPIO expander PCF8574 I2C connections, connect the SDA, SCL pins of PCF8574 to Arduino UNO’s SDA, SCL pins which are A4, A5 pins of the development board.
- As we know, we have to set the slave address of PCF8574 using A0, A1, A2 pins. And in this project we are going to use the slave address 0x20, therefore for that, we have to connect all pins to the ground. (As we have already seen in the above PCF8574 addressing image)
- In the next step, we are going to connect the buttons to Arduino digital pins D2, D3, D4, D5 as "Latte", "Cappuccino", "Espresso", "Cafe Mocha" flavors respectively and another terminal of the buttons is connected to ground. As we are going to use the buttons inactive low condition which means, when we press the button it will give us a logical LOW state.
- There may be a doubt in your mind why we have not used any PULL-UP resistors with buttons because we will handle that in our code. Arduino UNO comes with an internal PULL-UP resistor of 20-50 KOhms.
- Now connect the dc motors for each container, Water, Coffee, and Sugar container’s motors are connected with Arduino’s digital pins D10, D12, D11 respectively. Connect the coffee outlet motors for each type of Latte, Cappuccino, Espresso, Cafe Mocha with digital pins D6, D7, D8, D9 respectively. And at last, connect the mixer with the D13 pin.
- As we have mostly completed the wiring part, the first thing which we must make sure of before going to start our simulation is that all components should have adequate power supply and ground. And ground must be common in the whole circuit.
Now we hope you have understood the connections and you have already done it, so it is time to move to the coding part of our project.
Arduino Code for Smart Coffee Vending Machine
If you already know about the syntax and structure of Arduino sketch, it's a good thing, but if you have not been familiarized yet, no need to worry, we will explain it to you step-by-step.
Arduino coding language mostly follow the syntax and structure of C++ programming language, so if you are familiar with C++, then it would be like a cup of cake for you to understand the code but still if you don’t have any background knowledge, you don’t have to worry again, we have your back.
Arduino Coding follows a strict structure, it has mainly two sections. we have to write our code in those two functions.
As we are going to explain the Arduino code, it would be easy to understand if you have opened the code in the Arduino IDE already.
Declaration code:
- When we start our code, we will first include all the required libraries which we are going to use in this project.
- So our first step would be to download the required libraries if they are already not pre-installed in the Arduino IDE.
- Mainly we will use only two libraries, one for LCD display and the other for I2C communication.
- And I2C related functions come in the Wire library which will be pre-installed in Arduino ID, we don't have to install it explicitly.
- For the LCD module, we will use the Liquid Crystal_I2C library that we have to install.
- We can install libraries related to Arduino from the Arduino IDE by going to ‘Sketch > Include Library > Manage Library’. Now in the library manager, we can search for our required libraries. We can install the libraries using zip files also.
- >> Now, as we have installed all the required libraries. Let’s include them in our sketch.
- After that, we will define the pins which we are going to use in our project.
- We have to define them globally so that we can use them in all functions.
- You must be having a doubt why we have not defined pins for I2C.
- Because those pins are pre-defined in the Wire library, we can not assign any other pins for I2C communication.
- Now we will define and declare all the variables which are required in our project.
- There is an array for the price of a coffee with the size of 4, as we will only provide only 4 types of coffees and a string type variable for storing the name of flavors of coffee.
Arduino Setup() Function:
In this Arduino Setup() function, we will write a section of code that will only run once.
- So mostly we will write the declarations, define the type of pins and initialize the peripherals such as the LCD module.
- We want to take user input from the buttons therefore we will declare them as INPUT type.
- We have not connected PULL UP resistors in buttons as you have read above, we will handle that in the code therefore we have declared it as INPUT_PULLUP mode.
- We have declared motor pins as OUTPUT mode because we want to control the motors.
- After that we will initialize the LCD module then we will turn on the backlight of LCD, set the cursor to 0,0 index and using ‘lcd.print()’, we will print the welcome message on the LCD module.
- In the setCursor function, the first argument is used for X-Axis and the second argument is for Y-Axis.
- It will display the welcome message for 1 sec as we have given a delay for 1000 milliseconds after we clear the display.
Arduino Loop() Function:
Arduino Loop function runs after the the
‘void setup()’ function.
- In this section, we will write the code which is required to run in a continuous loop. So we will write our main application code here.
- So when the code reaches the void loop section, first we will display the flavor and the price of the coffee on LCD display as we want to show the user what type of coffee our vending machine makes and the price of those individually.
>> Now we will write the section for reading the user input from the buttons. As we have set that the condition will be true when the button will be logic LOW state.
>> Now when the user will press the button, the state of the button’s pin state will be changed to logic LOW state and then our ‘if condition’ will be true and code and our operation will enter in the ‘if condition’ section.
>> Here we will display to the user the current process stage of the coffee making. So we will clear the LCD display and then set the cursor to 0,0 index. After that we will display the message for collecting the ingredients.
- As we have not cleared the display, it will display the same message.
- After 1 second delay, we will start the water container motor for pouring the water for 2 seconds.
- Thereafter we will set the water’s container pin to LOW and Sugar’s container motor pin to HIGH for 2 seconds, similarly for the coffee’s container pin.
- Now we will start the motor for the selected flavor of coffee for 2 seconds and then stop it.
- As now our selected coffee is getting ready so we will display the message for the same.
- To display any new message, we have to clear our display with pre-occupied text.
- Now we will start the mixer motor for 10 seconds to mix all the poured ingredients.
>> Now our selected coffee is ready. So we will clear the LCD display and set the cursor, and will print the message regarding the prepared coffee with the price of it.
Results/Working:
- Below is the Flow diagram of coffee vending machine:
- Let’s understand the code with an example, we will go with the starting step.
- Power ON the device, the machine will display the welcome message that you can change from that code as per your choice.
- That message will be shown for 1 second thereafter it will clear the display.
- Now it will display the type of coffee as "Latte", "Cappuccino", "Espresso", "Cafe Mocha" and their respective prices.
- Let’s suppose, the user wants to have a Latte today, so he/she will press the button for the same, thereafter our coffee-making process will start.
- The first LCD display will show the message “Wait a Moment Collecting Ingredients” and it waits for 1 second.
- Thereafter it will start pouring the water for 2 seconds, then it will stop that motor.
- After that, it will start to pour sugar for 2 seconds, then stop that motor.
- At last, it will start to pour the coffee for 2 seconds, then stop that motor.
- It will start the motor of the selected type of coffee to dispense the coffee to the container and then it will wait for 1 second.
- Now LCD will display the message for coffee getting ready as "Wait a Moment Your’s Rich Latte is getting ready…” as the user has selected Latte that’s why it shows “Latte is getting ready… “.
- Now we will start the mixer to mix all the ingredients for 10 seconds.
- Again we will clear the LCD display to show the message for prepared coffee as “ Your's Rich Latte is ready. Please Collect it Your's Amount - 5/-”.
- Then it waits for 5 seconds and clears the display and again shows the price and the available types of coffee.
- As Proteus requires the hex file of the code to run the simulation.
- So for that, open the Arduino IDE and please verify your code before making a hex file by clicking on the ‘Verify’ button to remedy any errors.
- To get the hex file from the Arduino IDE click on “Sketch > Export Compiled Binary”.
- Your hex file will be generated successfully now put that hex file to the Arduino UNO board in the Proteus software.
- Everything is now in place, it's time to run the simulation and get a nice virtual coffee.
I hope you have understood the whole working of our smart vending machine project and enjoyed it as well. I think we have explained pretty much everything but still if you have any doubts or improvements please let us know in the comment section.
Thanks for giving your valuable time for reading it.
Introduction to Single Layer PCB
Hello friends, I hope everything's going well. Today, I am going to share the 13th chapter in the PCB learning series, where we will discuss the single-layer PCB in detail i.e. definition, construction, advantages, manufacturing, applications etc. So let’s try to absorb everything about the single-layer PCB:
Single-layer PCB overview:
- Just a quick recall, PCB stands for a printed circuit board having different electrical components connected with the help of pads and tracks of copper foil, incorporated on an insulating material(substrate).
- Single-layer PCBs have only one conductive layer of copper.
- The PCB board itself has a total of 3 layers in single-layer PCB other than the copper layer which are substrate, solder mask, and silkscreen.
- In the past, phenolic aldehyde was used as a substrate but nowadays glass fiber epoxy resin is used because of its flexibility with temperature variations.
Single-layer Definition
- Single-layer PCB refers to a printed circuit board that has only 1 layer of conductive pattern.
- Single-layer PCBs are simple, low-cost and can be designed at home.
- Different materials like glass fiber reinforced epoxy resin with copper foil and a paper mask having phenolic resin with copper foil are used in the manufacturing of single-layer PCB.
Pricing of Single Layer PCB
Now let's have a look at the pricing of Single Layer PCB. As Single Layer PCB is the simplest form of PCB, so it's quite low cost as compared to other PCB types. Let's take the example of JLCPCB Fabrication House, a well-renowned PCB manufacturing company, that offers competitive rates for PCB designing.
- We need to open JLCPCB official site and click on its Order Now page, as shown in the below figure:
- As you can see in the above figure, I have selected 1 for Layers, so I am ordering for Single Layer PCB.
- The size of the Single Layer PCB is 100x100mm and I have placed the order for 5 pcs of PCB.
- For this order, JLCPCB has given me a price of $2.00, so you can see it's quite cheap to design Single Layer PCB.
Construction of single layer
- In 1950, the first single-layer PCB was designed.
- The base material or substrate is made up of fiberglass and is compact in its sense.
- There is a copper layer that has conducting path for various competent on the boards above subtract. Needless to mention, different boards have different copper thicknesses consistent with your needs and demands, defined in ounces per sq. ft.
- On one hand, there is a solder mask layer on the top of the copper foil. The layer mainly protects the copper foil from insulating which avoids conduction in case direct contact happens with some conducting material.
- On the opposite hand, there's a silkscreen layer on the highest of all layers, which is especially in adding characters and symbols on the board, so it’s easy to have a far better understanding of the board.
Types of singles layer PCB
There are some types of single-layer PCB. We are going to explain them below concerning its manufacturing material.
- Single-layer rigid PCB
- Single-layer flexible PCB
- Single-layer rigid-flex PCB
- Single-layer high-frequency PCB
- Single-layer aluminum-backed PCB
1 Single-layer rigid PCB
- Single-layer rigid PCB is a type that is made up of a rigid material such as fiberglass.
- These PCBs are hard and prevent the circuit from bending and breaking.
- It's used in applications i.e. calculators, power supplies etc.
A single-layer rigid is shown in the figure below.
2. Single-layer flexible PCBs
- A single-layer flexible PCB has a flexible substrate like polyimide in its manufacturing.
- Single-layer flexible has so many advantages over single-layer rigid PCBs. But the cost is too high for its fabrication. A single-layer flexible PCB is shown below.
A single-layer flexible PCB
3. Single-layer high-frequency PCBs
- Circuits emitting a frequency in Gigahertz, single-layer high-frequency PCB is used.
- Polyphenylene oxide (PPO) or Teflon Material is used in single-layer high-frequency PCBs.
- If you are selecting High-frequency single-layer PCB, you should consider many aspects such as dielectric loss, thermal expansion, water absorption, etc.
- A single-layer high-frequency PCB is shown in the below figure
A single layer of high-frequency PCBs
4. Single-layer rigid-flex PCBs
- Single-layer rigid-flex PCB is a combination of both Rigid PCB and Flexible PCB.
- Single-layer rigid-flex PCBs have so many advantages over single-layer rigid and flexible PCBs such as it reduces the size and weight of the overall PCB.
- Single-layer rigid-flex is shown below figure.
A single layer rigid flexes PCBs
5. Single-layer aluminum-backed PCBs
- Single-layer Aluminium-backed PCB has an aluminum substrate.
- Aluminum-backed PCB is used with the thermal insulating material for the heat to dissipate by Aluminium.
- Single-layer aluminum-backed PCBs are shown below in Figure.
A single layer aluminum backed PCBs
Steps for the manufacturing process of PCB
- There are a lot of processes involved in the construction of a PCB.
- Almost 12-20 machines are used in the manufacturing of a simple single-layer PCB depending upon the demand of the customer and the requirement of the product.
- For ease of understanding, single layer PCB manufacturing process can be defined as
1. Cutting & Cleaning of PCB sheet:
- The circuit pattern is drawn on PCB using the photolithography technique in which warm iron is used to draw a pattern from photo paper to PCB.
- Photo paper is removed by washing PCB having photo paper on it.
- After drawing the pattern, check connecting nodes, jumpers, and docking points for additional components.
2. Etching with ferric chloride:
- prepare a solution of ferric chloride with water in a 1:3 ratio and dip board into it.
- The processing speed in this step is affected by the temperature of the solution and the thickness of the foil. You have to moderately heat the solution to speed up the process.
- Again clean the board with alcohol.
3. Drilling holes on the single layer PCB:
- now holes are drilled according to the requirement of the product. Clean the board again.
4. Soldering holes and lubricate sides of the board:
- at this stage, first of all, holes are soldered to make them able to make connections between components and layers.
- After soldering holes lubricating the sides of the board with a cover layer is done.
5. Testing of the final board:
- at this stage, the final prepared board is tested for whether it is ready or not.
6. Packaging:
- in the end, the final packaging is done and now the single layer PCB is ready to be delivered.
Common mistakes in single layer PCB manufacturing:
Following mistakes are made by designers during the manufacturing 9f the single-layer PCB.
1. incorrect conducting paths width:
- The maximum allowable width of the conducting path should be drawn to avoid voltage loss, overheating of the conductor, and low mechanical strength.
2. improper power circuit designs:
- When the width of the track is not made maximum then problems like output ripples, output voltage loss, and interference have to be faced. To avoid these problems track width should be maximum.
3. grounding problems:
- To avoid grounding problems, a separate insulating layer is used for wiring.
4. small gaps between copper:
- Gaps between copper conductors deposited on the board should not be so small, this can lead to the violation of the integrity of the board.
5. large no of holes on one plate:
- By increasing no of holes, no of conductive paths increases, and this, in turn, increases resistance.
Application for single-layer PCB
It is no doubt that single-layer PCBs are very simple. But single layer PCBs are used still in such a lot of complex devices. Some devices are listed below.
- Single-layer PCBs are used in digital cameras circuits.
- Single-layer PCBs used in coffee-making machine circuits.
- Single-layer PCBs are used in soiled state drives which are mostly used in the power industry.
- Single-layer PCBs are utilized in switching relays which are mostly utilized in the automotive and power industry.
- Single-layer PCB used in vending machine circuits.
- Single-layer PCBs used in digital calculators which are consist of only a single PCB.
- Single-layer PCB used in photocopy and printer machine circuits.
- Single-layer PCB is used in radio and stereo equipment circuits.
- Single-layer PCB is utilized in digital microwave timer circuits to modify on or off the oven timely.
- Single-layer PCB is used in led lighting circuits for making power light circuits.
- Single-layer PCB used in digital and analog power supplies circuits.
- Single-layer PCB used in surveillance machine circuits.
- Single-layer PCBs are used in sensors products circuits.
- Single-layer PCBs are used in packing machines to achieve the high targets of fast-packing and are mostly used in packing industries.
- Single-layer PCBs are used in timing circuits to switch on or off the machine timely.
Advantages of single-layer PCB
There are some advantages given below of single-layer PCB.
- The single-layer PCs is very easy to design and has a lower probability to make incorrect design because the single-layer PCBs is very simple.
- Its price is very less especially when it is ordered in bulk quantity as compared to the other types of PCBs.
- It is easy to understand for anyone because it is a very simple circuit
- Because its components are installed on only one side there for its a required lower jumper o compensate for the circuit.
- Its drilling, soldering, de-soldering, and components inserting process is very easy because single layer PCBs only consist of a single layer.
- Its design circuit required a very short time to make a simple design.
- There is less probability of short-circuiting and producing noise because its components are installed at some distance from each other.
- For fault tracing and repairing of this single layer PCB need less time.
- When we compare the single-layer PCBs to the other types of PCBs. It is more reliable and efficient.
- The installation of single-layer PCBs is very easy.
Disadvantages of single layer PCB:
- Single-layer PCBs have many advantages regarding cost, efficiency, and ease of installation but they still can not be used for any circuit because of their limitations. The limitations or disadvantages of using single layer PCB are discussed below:
The simplistic design and small space:
- Single-layer PCB has a very simple design and this simplistic design presents as the hurdle in complex devices that require a lot of components and connections.
- To mount a large no of components, large space is required but single layer PCBs don’t have enough space to mitigate this problem.
Slow speed and low operating capacity:
- As they have a limited no of components so their speed is slow, their power is also slow.
- Due to their low speed, they have low operating capacity.
Large size and high weight:
- To add components to the single-sided PCB you need to enhance PCB into its dimensions. As it is single-sided so that’s why we can not add layers. By enhancing PCB into its dimensions, the size of PCB.
- The enhanced size and large no of components ultimately enhance the weight of single-layer PCB.
Conclusion:
- At the end of it all, we can conclude that single-layer PCBs have unique advantages, benefits, and applications but as an end-user, you always have to choose which type of PCB you need.
- In modern technology advancements, single-layer PCBs are now in an underestimated situation but are still used.
- With a low budget and volumes of specific design single-layer PCB is the right choice when compared to the other types of PCB.
That’s all for today’s article. I hope you have enjoyed the article and made grip on the understanding points. However, if you still face any skepticism regarding single-layer PCB then please feel free to leave your questions in the comment section. I will provide an answer to these questions to the best of my knowledge and research skills. Also, provide us with your innovative feedbacks and suggestions you improve the quality of our work and provide you content according to your needs and expectations. Stay tuned! Thank you for reading this article.
How to use ADC with STM32?
An Analog to Digital Converter (ADC) converts a continuous signal (usually a voltage) into a series of discrete values ??(sequences of bits). The main features are:
- Resolution (in analog terms): It is the minimum variation of the analog input voltage that can determine the variation of the LSB, that is of the least significant bit of the output code. Since the quantization step Q corresponds to the LSB, it can be said that the resolution coincides with the quantization step Q (and therefore is measured in Volts). We can say that the quantization step Q corresponds to the LSB because two contiguous quantization bands, each of amplitude Q, are identified by codes that differ only for the least significant bit.
- Resolution (in digital terms): It is the number n of bits present at the converter output, that is the number of bits with which the converter encodes a sample of the analog input signal. As the number of bits of the converter increases, the number of quantization bands increases and (with the same full-scale value VFS) their amplitude decreases, an amplitude which is nothing more than the step Quantization Q. If the quantization step narrows, the smaller the voltage variation necessary to determine the variation of the LSB, i.e., of the least significant bit of the code, becomes the exit. So, saying that a converter has many bits is equivalent to saying that the voltage variation necessary to make the LSB vary is small. The image below shows the 3 bits ADC input-output characteristics.
- Full-scale voltage: It is the range, that is the maximum excursion, of the input voltage. Typical dynamic values are between 10 Vpp ( pp peak to peak) and 20 Vpp, unipolar or bipolar.
- Types of response: in general, ADCs have a response of a linear theoretical type of response, but there are also types with a logarithmic response.
- Accuracy: indicates the goodness of the conversion depends on it. The error made by the ADC is usually measured. This error consists of two components: a quantization error and a non-linearity error.
- Sampling frequency: A sampling is an operation with which the input signal is discretized over time, transforming it into a succession of values, samples in fact, which will subsequently be digitized. The simplest way to extract values is to use a switch, in series with the signal, which closes and opens at defined and equidistant intervals. The smaller this interval, called the sampling step (Ts), the more faithful the reconstruction of the signal will be starting from its samples. Likewise, too small a sampling step leads to a waste of resources (measurement time, memory for data storage). A sampling of the signal generally indicates not only its discretization over time but also its maintenance until the next closing of the circuit-breaker. These two phases are realized by special circuits called Sample & Hold (S / H).
There are different types of ADCs, the most common are listed below (illustrating their operation is not the purpose of this article):
- A direct conversion ADC (Flash ADC)
- A Successive Approximation Register (SAR) ADC
- One dual ramp ADC (Dual Slope or integration)
- A pipeline ADC
- A tracking ADC (delta-coded)
Generally, STM32 microcontrollers have at least one ADC (a SAR ADC) with the following characteristics:
- Resolution: ADCs have a resolution of up to 12 bits with a maximum conversion frequency of 2.5 MHz, with 18 multiplexed channels among which 16 can be available for measurements of external signals, the other two are for internal measurements (temperature and voltage).
- Conversion Time and Conversion Groups: The conversion time can be individually programmed for each channel. There are 8 discrete times conversions for each ADCCLK clock frequency (Fc), these times range from 1.5 to 239.5 cycles.
Fc = ADCCLK / (12.5 + Number of cycles)
Each ADC has two conversion modes: “regular” and “injected”.
- The "regular" mode allows you to specify a channel or a group of channels to be converted in turn one after the other. The conversion core can consist of more than 16 channels, and the order in which the channels must be converted can also be programmed. The conversion can be initiated by software or by a hardware event consisting of a series of timer signals or by line 1 of the EXTI. Once the conversion has started, you can carry out continuous conversions, or you can operate discontinuously by converting a selected number of channels and then stopping the conversion pending the triggering of the next core. At the end of a conversion the result is stored in a single register (result register) and an interrupt can be generated. The ADC1 has a dedicated DMA channel that can be used for transferring the converted value from the result register to a memory buffer. Through this method, an entire conversion cycle can be copied into memory, eventually obtaining a single interrupt generated by the DMA. To further speed up the conversion, a double-sized buffer can be used to generate two interrupts: one when the first half has been filled (first conversion cycle) and the other when the second half is filled (second conversion cycle). This mode can be combined with the "DMA circular buffer mode" to handle multiple conversions with hardware.
- The second conversion mode is called the “injected group”. It is able to carry out the conversion sequence up to a maximum of four channels, which can be triggered by a software or hardware event. Once triggered, it will stop the conversion of the regular group, carry out its sequence of conversion and then will allow the regular group to continue the conversion. A conversion sequence can be configured in this mode. Unlike the regular group, in this mode, each result has its own register (result register) and its own offset register. This last register can be programmed with a 16-bit value automatically deducted from the ADC result.
Furthermore, the "Dual Conversion Modes" can be active:
In the STM32 with almost two ADCs and it is, therefore, possible to perform different conversion modes: in these types of conversion the ADC2 acts as a slave while the ADC1 acts as a master allowing 8 different types of conversion.
- Injected Simultaneous Mode and Regular Simultaneous Modes: These two modes synchronize the regular and injected group conversion operations on two converters. This is very useful when two quantities (current and voltage) have to be converted simultaneously.
- Combined Regular / Injected Simultaneous Mode: This mode is a combination of both the regular and injected modes and allows us to have a synchronized conversion sequence.
We are now ready to write a first simple example using the ADC peripheral. The goal is to measure the voltage in a voltage divider composed of a fixed value resistor and a potentiometer (so that by moving the potentiometer cursor, the voltage to be read varies) we begin by configuring our peripheral with STCube Tool. For this project, we will use the NUCLEO STM32L053R8. This board has only one ADC with 16 channels and a resolution of up to 12bit.
Now we’ll see the configuration step by step:
| Where To Buy? |
|---|
| No. | Components | Distributor | Link To Buy |
| 1 | STM32 Nucleo | Amazon | Buy Now |
ADC channel selection
We have to flag IN0 to activate Channel 0, then we can configure the peripheral. Channel 0 is on GPIO PA0 as we can see in the picture below:
ADC setting
We select the ADC_prescaler equal to 4, resolution to 12bit (maximum of a resolution, we can choice between 6, 8, 10 and 12 bits), “right data alignment” (we can choose between right and left alignment), and “forward” as scan direction (we can choose between forward and backward).
For this first example we’ll hold disabled Continuous, Discontinuous conversion and DMA mode. Furthermore, the ADC sets, at the end of single conversion, the EoC (End of Conversion) flag.
ADC Regular conversion mode
We select 12.5 Cycles as sampling time (in this way the sampling frequency is 320 kHz obtained from the formula described above), the start of conversion is triggered by software. Furthermore, for this application the watchdog is disabled.
After the generation of the initialization code with STCube, we can find in our project the ADC configuration. As for every peripheral, the HAL library defines the dedicated C structure, for the ADC defines “ADC_HandleTypeDef”.
In our case the “ADC1” is the instance that points to our ADC. The structure “ADC_InitTypeDef” is used to handle the configuration parameters. In our example is generated as follow:
static void MX_ADC_Init(void)
{
/* USER CODE BEGIN ADC_Init 0 */
/* USER CODE END ADC_Init 0 */
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN ADC_Init 1 */
/* USER CODE END ADC_Init 1 */
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc.Instance = ADC1;
hadc.Init.OversamplingMode = DISABLE;
hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
hadc.Init.Resolution = ADC_RESOLUTION_12B;
hadc.Init.SamplingTime = ADC_SAMPLETIME_12CYCLES_5;
hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc.Init.ContinuousConvMode = DISABLE;
hadc.Init.DiscontinuousConvMode = DISABLE;
hadc.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc.Init.DMAContinuousRequests = DISABLE;
hadc.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc.Init.LowPowerAutoWait = DISABLE;
hadc.Init.LowPowerFrequencyMode = ENABLE;
hadc.Init.LowPowerAutoPowerOff = DISABLE;
if (HAL_ADC_Init(&hadc) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel to be converted.
*/
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC_Init 2 */
/* USER CODE END ADC_Init 2 */
}
The function
HAL_ADC_MspInit(ADC_HandleTypeDef* hadc) needs to initialize the peripheral and define the clock and the GPIO ( in our case PA0).
void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(hadc->Instance==ADC1)
{
/* USER CODE BEGIN ADC1_MspInit 0 */
/* USER CODE END ADC1_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_ADC1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**ADC GPIO Configuration
PA0 ------> ADC_IN0 */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN ADC1_MspInit 1 */
/* USER CODE END ADC1_MspInit 1 */
}
}
/**
* @brief ADC MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param hadc: ADC handle pointer
* @retval None
*/
The function
HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc) needs to de-initialize the peripheral.
void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
{
if(hadc->Instance==ADC1)
{
/* USER CODE BEGIN ADC1_MspDeInit 0 */
/* USER CODE END ADC1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_ADC1_CLK_DISABLE();
/**ADC GPIO Configuration
PA0 ------> ADC_IN0
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0);
/* USER CODE BEGIN ADC1_MspDeInit 1 */
/* USER CODE END ADC1_MspDeInit 1 */
}
}
Before describing the code let's see how to make the connections on the development board.
We need a 10kOhm potentiometer and a 2kOhm resistor. The potentiometer is connected between 3.3V and 2kOhm resistor, the common point is connected to PA0, and finally, the other end of the 2k Ohm resistor is connected to the ground pin.
Acting on the potentiometer we will see the read voltage vary from 3.3 Volt to about 0 Volt.
Now let's dive into the code:
In the Includes section we add the header file of main.
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
In “Private variables” section we find “ADC_HandleTypeDef hadc” as previous said is an instance to C structure to handle the ADC peripheral. Then, we add three variables:
- Resolution defines the number of steps used by ADC (12bit = 2^12 -1= 4095) is a constant integer;
- vs defines the maximum voltage to read, is a constant float;
- volt is the variable where the voltage value read by the ADC is store ( is a float variable)
/* Private variables -----------------------*/
ADC_HandleTypeDef hadc;
/* USER CODE BEGIN PV */
const int Resolution = 4095;
const float Vs =3.300;
float volt;
/* USER CODE END PV */
Then, we can find the protype of function to handle the peripherals and resources initialized (system timer, GPIO, and ADC).
/* Private function prototypes -----------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_ADC_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
Finally, the main starts.
In the first part we call functions to initialize the peripherals and resources used:
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_ADC_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
In the second part, that is, inside an infinite loop (while (1)) there is the function to start the conversion of the ADC, read the data and save it in the variable volt and finally stop the conversion wait for a second and start with the conversion and so on.
while (1)
{
/* USER CODE END WHILE */
HAL_ADC_Start(&hadc);
if(HAL_ADC_PollForConversion(&hadc,10)==HAL_OK)
{
volt=HAL_ADC_GetValue(&hadc)*Vs/Resolution;
}
HAL_Delay(1000);
HAL_ADC_Stop(&hadc);
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
Now, once our code has been compiled, we can debug it in real-time, just press the "spider" icon (see figure below) and see how the volt variable varies by acting on the potentiometer.
Once we have clicked on the debug button, at the top right, we can select the "live expression" window and add (by writing the name in the table) the variable to be monitored.
Now we can start the debug by clicking on the “Resume” button (on the top right) or by pressing the F8 key (on our keyboard).
We are now ready to read our voltage value. We will see that by acting on the potentiometer we will read the voltages in the whole range considered.
Some measures are shown below:
- First reading volt=1.3188 Volt
- First reading volt=3.29919 Volt
- First reading volt=1.02158 Volt
So, that was all about How to handle ADC in STM32 Microcontrollers. In the next tutorial, we are going to work on STM32 DAC operations. Till then take care !!!
STM32 SPI Communication
The SPI (Serial Peripheral Interface) protocol, or rather the SPI interface, was originally devised by Motorola (now Freescale) to support their microprocessors and microcontrollers. Unlike the I2C standard designed by Philips, the SPI interface has never been standardized; nevertheless, it has become a de-facto standard. National Semiconductor has developed a variant of the SPI under the name Microwire bus. The lack of official rules has led to the addition of many features and options that must be appropriately selected and set in order to allow proper communication between the various interconnected devices. The SPI interface describes a single Master single Slave communication and is of the synchronous and full-duplex type. The clock is transmitted with a dedicated line (not necessarily synchronous transmission that has a dedicated line for the clock) and it is possible to both transmit and receive data simultaneously. The figure below shows a basic connection diagram between two peripherals that make use of the SPI interface.
From the figure, it is immediately possible to notice what has just been said, namely that the communication generally takes place between a Master and a Slave. The interface presents 4 connection lines (excluding the ground however necessary), for which the standard SPI is also known as 4 Wire Interface. The Master starts the communication and provides the clock to the Slave. The nomenclature of the various lines in the SPI interface is normally as follows:
- MOSI: Master Output Slave In. Through this line the master sends the data to the selected slave;
- MISO: Master Input Slave Output. Through this line the slave sends the data to the master;
- SCLK: Serial Clock is generated by the master device, so it is the master starts the communication and the clock synchronizes the data transfer over the bus. The SPI clock speed is usually several MHz (today up to 100 MHz);
- SS: Slave Select or CS (Chip Select) generated by the master to choose which slave device it wants to communicate with (it must be set to a low logic level). SS (or CS) is not indispensable in all applications.
In addition to this standard nomenclature, there are other acronyms.
For example:
- The MOSI line is also called: SDO (Serial Data Out), DO (Data Out), DOUT and SO (Serial Out)
- The MISO line is also called: SDI (Serial Data In), DI (Data In), DIN and SI (Serial In)
- The Clock line is also called: CLK, SCK (Serial Clock).
- The Enable line is also called: CS (Chip Select), CE (Chip Enable)
The first advantage in SPI communication is faster communication, instead, the first disadvantage is the presence of the SS pin necessary to select the slave. It limits the number of slave devices to be connected and considerably increases the number of lines of the master dedicated to SPI communication as the connected slaves increase.
To overcome these problems, the devices in the daisy chain can be connected (output of a device connected to the input of the next device in the chain) as shown in the figure below where a single slave selection line is used.
The disadvantages, however, are the lower updating speed of the individual slaves and signal interruption due to the failure of an element.
We can use this communication to put in communication our micro-controller with different peripherals as Analog-Digital Converters (ADCs), Digital-Analog Converters (DACs), EEPROM memories, sensors, LCD screen, RF module, Real Time Clock, etc.
The STM32 micro-controllers provide up to 6 SPI interfaces based on the type of package that can be quickly configured with STCube Tool.
STCube Tool initializes the peripherals with HAL (Hardware Abstraction Layer) library. The HAL library creates for SPI (as all peripherals) an C structure:
It is so defined:
Where the main parameters are:
- • Instance: is the pointer variable it describes the SPI that we want to use. If we use SPI1, the name of the instance is SPI1.
- • Init: is an instance that points to the structure ( SPI_InitTypeDef) used to initialize the device. We will discuss the structure SPI_InitTypeDef shortly.
- • pTxBuffPtr, pRxBuffPtr: are pointer variables that point to an internal buffer. They are used to store the data during the communication when the programmer handles the SPI in interrupt mode (we will see forward)
- • hdmatx, hdmarx: are the pointer variable to instances of the DMA_HandleTypeDef struct. They are used when the programmer handles the SPI in DMA mode (will see forward).
As just said to initialize the SPI peripheral to be used, it is necessary to use the struct SPI_InitTypeDef. It is defined as follow:
When we use the STcubeMX to initialize the SPI peripheral we are modifying this structure
In details:
- Mode specifies the SPI operating mode, and Direction specifies the SPI bidirectional mode state. It is very easy to configure in STCubeMx. If we want to configure the SPI1. We can find SPI windows in Pinout&Configuration -> Connectivity. Here we can select between the SPI available. Now is possible to select the communication mode (Master, Slave, half-duplex, full-duplex, etc.) as follow:
If the slave supports, the full-duplex communication can be enabled.
- DataSize indicates the SPI data size. The user can select 8bit or 16bit.
- CLKPolarity defines if the serial clock steady state is LOW or HIGH.
- CLKPhase defines if the bit capture (trigger) takes place when the clock is on the falling edge or rising edge.
- NSS: if selected "Output Hardware" the slave select signal is managed by hardware otherwise is managed by software using the SSI bit.
- BaudRatePrescaler can be select the Baud Rate prescaler value.
- FirstBit indicates if data transfers start from Most Significant Bit (MSB) or Last Significant Bit (LSB).
- TIMode specifies if the TI mode is enabled or not.
- CRCCalculation: to enable to activate the CRC calculation.
- CRCLength: to define the length of CRC data.
- CRCPolynomial specifies the polynomial (X0+X1+X2) used for the CRC calculation. This parameter is an odd number 1 and 65535.
By enabling the SPI and the chip select pin, the pins available on the microcontroller are automatically chosen to manage this interface (but they can be changed by looking for the alternative functions of the different pins of the microcontroller). For example, in our case the following pins are selected:
- PA4 SP1_NSS
- PA5 SP1_SCK
- PA6 SP1_MISO
- PA7 SP1_MOSI
Now you can generate the initialization code. Before being able to write the first code to manage this communication interface, it is necessary to understand the functions that the libraries provide and the different communication modes.
As for other communication interfaces, the HAL library provides three modes to communicate: polling mode, interrupt mode, and DMA mode.
| Where To Buy? |
|---|
| No. | Components | Distributor | Link To Buy |
| 1 | STM32 Nucleo | Amazon | Buy Now |
STM32 SPI Communication in Polling Mode
Using the SPI in Polling Mode is the easiest way, but it is the least efficient way as the CPU will remain in a waiting state for a long time. HAL library provides the following functions to transmit and receive in polling mode:
- HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
Master receives data packets in blocking mode (polling mode).
The parameters are:
- hspi is a pointer to a “SPI_HandleTypeDef” structure. “SPI_HandleTypeDef” structure includes the configuration information for SPI module.
- pData is a pointer to data buffer
- Size is the amount of data to be sent
- Timeout is the timeout duration
- HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size, uint32_t Timeout)
Master transmits data packets in blocking mode (polling mode).
If the slave device supports, the full-duplex mode:
- HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size, uint32_t Timeout)
Master transmits and receives data packets in blocking mode (polling mode).
The parameters are:
- hspi is a pointer to a “SPI_HandleTypeDef” structure. “SPI_HandleTypeDef” structure includes the configuration information for SPI module
- pTxData is a pointer to transmission data buffer
- PRxData is a pointer to reception data buffer
- Size is the amount of data to be sent
- Timeout is the timeout duration
STM32 SPI Protocol in Interrupt Mode
Using the SPI in Interrupt Mode, also called non-blocking mode. In this way, the communication can be made more effective by enabling the interrupts of the SPI in order to receive, for example, signals when the data has been sent or received. This improves CPU time management. In applications where all the management must be deterministic and it is not known when an interrupt can arrive, these can potentially manage the time management of the CPU, especially when working with very fast buses such as SPI. We can enable the SPI interrupts directly during the initialization with STCube Mx.
HAL library provides the following functions to transmit and receive in interrupt mode:
- HAL_SPI_Receive_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
Master receives data packets in non-blocking mode (interrupt mode).
The parameters are:
- hspi is a pointer to a “SPI_HandleTypeDef” structure. “SPI_HandleTypeDef” structure includes the configuration information for SPI module
- pData is a pointer to data buffer
- Size is the amount of data to be sent
To handle the interrupt needs to write our code in the callback:
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef * hspi)
{
// Message received .. Do Something ...
}
- HAL_SPI_Transmit_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
Master transmits data packets in blocking mode (interrupt mode).
To handle the interrupt needs to write our code in the callback:
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef * hspi)
{
// Message transmitted.... Do Something ...
}
If the slave device supports, the full-duplex mode:
- HAL_SPI_TransmitReceive_IT(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size)
Master transmits and receives data packets in non-blocking mode (interrupt mode).
To handle the interrupt needs to write our code in the callback:
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef * hspi)
{
// Message transmitted or received.. .. Do Something ...
}
STM32 SPI Communication in DMA Mode
Using the SPI in DMA Mode the SPI bus can be used at its maximum speed, in fact, since the SPI must store the received and transmitted data in the buffer to avoid overloading it is necessary to implement the DMA. In addition, use by DMA mode frees the CPU from performing "device-to-memory" data transfers. We can easily configure the DMA during the initialization using STCubeMx :
In this case, the DMA is enabled in normal (we can use it in circular mode) mode both in transmission and reception
HAL library provides the following functions to transmit and receive in DMA mode:
- HAL_SPI_Receive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
Master receives data packets in non-blocking mode (DMA mode).
The SPI device receives all bytes of data in the buffer one by one until the end in DMA mode. At this point, the callback function will be called and executed where something can be done.
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef * hspi)
{
// Message received .. Do Something ...
}
- HAL_SPI_Transmit_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size)
Master transmits data packets in non-blocking mode (DMA mode).
The SPI device sends all bytes of data in the buffer one by one until the end in DMA mode. At this point, the callback function will be called and executed where something can be done.
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef * hspi)
{
// Message transmitted….. Do Something ...
}
If the slave device supports, the full-duplex mode:
- HAL_SPI_TransmitReceive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size,)
Master transmits and receives data packets in non-blocking mode (DMA mode).
The SPI device sends or receives all bytes of data in the buffer one by one until the end in DMA mode. At this point, the callback function will be called and executed where something can be done.
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef * hspi)
{
// Message transmitted or received.... Do Something ...
}
We are now ready to handle an SPI communication with STM32.
Write and Read an I2C EEPROM with STM32
EEPROMs (Electrically Erasable Programmable Read-Only Memories) allow the non-volatile storage of application data or the storage of small amounts of data in the event of a power failure. Using external memories that allow you to add storage capacity for all those applications that require data recording. We can choose many types of memories depending on the type of interface and their capacity.
EEPROMs are generally classified and identified based on the type of serial bus they use. The first two digits of the code identify the serial bus used:
- Parallel: 28 (for example 28C512) much used in the past but now too large due to having many dedicated pins for parallel transmission
- Serial I2C: 24 (for example 24LC256)
- Serial SPI: 25 (for example 25AA080A)
- Serial - Microwire: 93 (for example 93C56C-E/SN)
- Serial – UN I/O: 11 (for example 11LC040-I/SN)
Now we will see how to write or read data on an I2C EEPROM like 24C256C. This serial EEPROM is organized as 32,768 words of 8 bits each. The device’s cascading feature allows up to eight devices to share a common 2-wire bus. It is available in various 8-pin packages
The device can be used in applications consuming low power. The device is available in all standard 8-pin packages. The operating voltage is comprised of between 1.7V and 5.5V.
- Serial Clock (SCL) is an input pin used to control data flow. On the positive-edge clock, the data is inserted into the EEPROM device, while on the negative edge clock, the data is processed out of the EEPROM module.
- Serial Data (SDA) is a bidirectional input-output for serial data transfer. It is an open-drain pin.
- Device Addresses (A2, A1, A0) are input pins to set the device address. These pins allow you to customize the address of the device within the I2C bus. They must connect directly to GND or to VCC (hard wired). If these pins are left floating, the A2, A1, and A0 pins will be internally pulled down to GND. When using a pull-up resistor, it recommends using 10kOhm or less.
- Write Protect (WP) is an input pin. We can perform normal writing operations, by connecting it to GND; When connected directly to VCC, all write operations to the memory are restricted. If this pin is left open/floating, it will be pulled down to the GND(internally). When using a pull-up resistor, it recommends using 10kOhm or less.
- Device Power Supply (VCC)
- Ground (GND)
In our example, we connect A0, A1, A2 directly to VCC in this way the device address is 1010111 (in general A0, A1, A2 identify the last three significant bits of the device address 1 0 1 0 A2 A1 A0) is 0x57 in Hexadecimal. The 4 most significant bits are preset (Control Code), the A0, A1, A2 are Chip Select Bits.
Now we start with our project using STNucleoL053R8 and STCube to generate the initialization code. Below is shown the connection
- A0, A1, A2 are connected directly to VCC in this way the device address is 1010111 (in general A0, A1, A2 identify the last three significant bits of the device address 1 0 1 0 A2 A1 A0) is 0x57 in Hexadecimal.
- WP is connected to the ground to allow the normal write operation
- SCL and SDA are connected to PA8 and PA9 respectively of STM32L053R8
So, we configure the I2C1 using STCube and leave all configuration as it is and we will operate in polling mode.
In GPIO setting select PA9 (SDA) and PA8 (SCL).
Now we create a new STM32CubeMX project with the following steps:
- Select File > New project from the main menu bar. This opens the New Project window.
- Go to the Board selector tab and filter on STM32L0 Series.
- Select NUCLEO-L053R8 and click OK to load the board within the STM32CubeMX user interface
Then the tool will open the pinout view.
- Select Debug Serial Wire under SYS, for do it click on System Core (on the topo right) and then select SYS and finally flag on “Debug Serial Wire”.
- Select Internal Clock as clock source under TIM2 peripheral. To do this click on Timers and then select TIM2. Now go to clock source and select through the drop-down menu “internal clock”.
- Select and enable in “Connectivity” the I2C1 and left all configuration as it is and we will operate in polling mode.
- Configure in GPIO setting PA9 (SDA) and PA8 (SCL) to manage the I2C communication.
- Check that the signals are properly assigned on pins:
- SYS_SWDIO on PA13
- TCK on PA14
- SDA I2C1on PA9
- SCL I2C1on PA8
- Go to the Clock Configuration tab and no change the configuration in order to use the MSI as input clock and an HCLK of 2.097 MHz.
- Select Timers -> TIM2 and change the Prescaler to 16000 and the Counter Period to 1000.
- In the Project Manager tab, configure the code to be generated and click OK to generate the code.
Our project has been initialized by STCubeMX. In the /Core/Src/main.c we will find our main where we will write the main body of our program.
Now let’s see what the code generator did:
First of all, we find the “Include” section we can add the library needed.
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
In our case we can add also “stm32l0xx_hal.h” library to be able to use HAL library (I2C HAL library included)
#include "stm32l0xx_hal.h "
#include "Var.h "
#include "Funct.h "
In “Private variables” has been defined two privates variable htim2 and hi2c1;
- - htim2 as first parameter an instance of the C struct TIM_HandleTypeDef;
- - hi2c1 as first parameter an instance of the C struct UART_HandleTypeDef.
/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim2;
UART_HandleTypeDef hi2c1;
unsigned short int address; // eeprom address
unsigned char EEP_pag = 0x00 // EEPROM page
unsigned char EEP_pos = 0x00 // EEPROM position
unsigned char rdata = 0x00 // to store the data read from EEPROM
In “Private function prototypes” we find the protype of function to initialize the System Clock, GPIO, timer and peripheral:
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM2_Init(void);
static void MX_I2C1_Init(void);
This function has been generated automatically by STCubeMx with the parameter selected.
The main contains the initialization of the peripherals and variables, before the while loop the code call the function Write_EEPROM() and Read_EEPROM() to write and read a data in a specific address of the EEPROM. these functions were written in EEPROM.c, a C file added to our project in the src folder.
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM2_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
address = 0x00 << 8 | 0x00 // eeprom page 0 , position 0
// Now we want to store 10 in page 0x00 and position 0x00 of EEPROM
Write_EEPROM(address, 10, 0)
// Now we want store in rdata variable the content of cell memory 0x0000
rdata = Read_EEPROM(address, 0)
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
}
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
Furthermore, we have added two header files and one c file:
- Var.h contains the declaration of global variables:
/** Var.h
* Created on: 27 ott 2021
* Author: utente */
#ifndef INC_VAR_H_
#define INC_VAR_H_
extern unsigned char buf[20];
extern int i;
#endif /* INC_VAR_H_ */
- Funct.h contains the prototype of the user function
/** Funct.h
* Created on: 28 ott 2021
* Author: utente */
#ifndef INC_FUNCT_H_
#define INC_FUNCT_H_
extern unsigned char Read_EEPROM(unsigned int, unsigned char);
extern void Write_EEPROM(unsigned int, unsigned char, unsigned char);
#endif /* INC_FUNCT_H_ */
- EEPROM.c contains the function written by the user to handle the writing and reading operation with EEPROM:
- unsigned char Read_EEPROM(addr, device) reads from cell memory address (addr)and store the content in dato.
- void Write_EEPROM(addr, dato, device) writes data (dato) to memory address (addr).
/** Serial.c
* Created on: Oct 29, 2021
* Author: utente */
#include "stm32l0xx.h" // Device header
#include "stm32l0xx_hal_conf.h"
#include "stm32l0xx_hal.h "
#include "Var.h "
#include "Funct.h "
extern I2C_HandleTypeDef hi2c1;
unsigned char Read_EEPROM(unsigned int addr, unsigned char device)
{
unsigned char page;
uint8_t dato;
page=0xAF; // due to chip select bits setting
HAL_I2C_Mem_Read(&hi2c1,page, addr, I2C_MEMADD_SIZE_16BIT, &dato,1,5);
return dato;
}
void Write_EEPROM(unsigned int addr, unsigned char dato, unsigned char device)
{
unsigned char page;
page=0xAF; //due to chip select bits setting
HAL_I2C_Mem_Write(&hi2c1,page, addr, I2C_MEMADD_SIZE_16BIT, &dato,1,5 );
while(HAL_I2C_IsDeviceReady(&hi2c1, 0xA0, 1, HAL_MAX_DELAY) != HAL_OK);
HAL_Delay(10);
}
Now we are ready to compile and run the project:
- Compile the project within IDE.
- Download it to the board.
- Run the program.
So, that was all for today. I hope you have enjoyed today's lecture. In the next tutorial, we will have a look at How to perform SPI Communication with STM32. Till then take care and have fun !!! :)
Designing Logic Gates in PLC Simulator
Hello friends, I hope you all are doing great. In today's tutorial, we are going to design logic gates in PLC Simulator. It's our 4th tutorial in Ladder Logic Programming Series. We come today to elaborate the logic gates with comprehensive details for their importance in PLC programming. you can consider logic gates as the building blocks of ladder logic programming. Like every time we start with telling what you guys are going to have after completing this session? For those who like to buy their time and calculate for feasibility, I’d like to say by completing this article, you are going to know everything about what types of logic gates, how they are designed and how they work, how you can translate the logic in your head into the logic gate and some about logic calculation which is so-called logic algebra, and for sure the connection with examples between logic gates and the Ladder logic programming. In our previous tutorial, we have Created First Project using Ladder Logic, where we designed a simple logic by using contact and coil. Today, we are going to extend that code and will design different logic gates in ladder logic.
We are discussing these logic gates because they are the main building block of complicated logic. Normally, complex logic is designed using multiple logic gates. So, today, we will simulate the basic logic gates i.e. AND, OR, and NOT, while in the next lecture, we will simulate NAND, NOR, XOR and XNOR in PLC Simulator. So, let's get started:
Logic gates
In very simple language, it is a Boolean decision that has one of only two values either “TRUE” or “FALSE”, not both. For instance, the decision to run or shut down a motor, open or close a valve etc. Well! For deciding such Boolean nature thing, there are two things, inputs and logic to apply on those inputs. On the other way, logic gates apply some sort of logic to the inputs to determine the state of the output.
Truth table
It’s a table that lists all possible combinations of the inputs and the state of the output for each record. For example, a gate with two inputs has four possible combinations of the inputs and four states of the output. inputs.
Basics of logic gate
There are seven basic logic gates. Some of them have only one input while others have two inputs. There are seven basic logic gates which are “AND”, “OR”, “NOT”, “NOR”, “XOR”, “XNOR”, and “NAND”. So let us enjoy a short journey with them having a fast stop at each one’s station. Our trip will include, how they work, design, timing diagram, and connection with ladder logic programming.
Simulating ANR, OR, and NOT logic
- The AND, OR, and NOT logic are considered the basic building block logic for designing the complicated logic to decide the output status.
- By using two switches A and B and one output representing lamp or MOTOR, we can design and program these logics and simulate them on the PLCSIM simulator.
- Table 1 lists the truth table of the three logic AND, OR, and NOT.
Table 1: Truth table of the AND, OR, NOT logic
| Switch A |
Switch B |
Motor |
| AND LOGIC |
| 0 |
0 |
0 |
| 1 |
0 |
0 |
| 0 |
1 |
0 |
| 1 |
1 |
1 |
| OR LOGIC |
| 0 |
0 |
0 |
| 0 |
1 |
1 |
| 1 |
0 |
1 |
| 1 |
1 |
1 |
| NOT LOGIC |
| Switch |
Output |
| 0 |
1 |
| 1 |
0 |
The “AND” Logic Gate
The “AND” logic gate has two inputs and one output. Like its name, the only condition for having the output become true, is by having both inputs, input A and input B are true. Table 1 lists the truth table of the “AND” gate and Fig. 1 images the symbol of the “AND” gate. In addition, Fig. 2 shows a sample of ladder logic rung that uses “AND” gate logic. It decides the status of the motor based on two switches. The two switches must be in true status for running the motor. ‘to sum up, the logic of the “AND” gate, is that, the output comes to true when and only when both inputs A and B are true.
Table 1: the truth table of “AND” logic gate
| Input A |
Input B |
Output |
| False |
False |
False |
| True |
False |
False |
| False |
True |
False |
| True |
True |
True |
Fig. 1: symbol of “AND” logic gate [1]
In the ladder logic rung shown in Fig. 2, there are two contacts I1 and I2, they are of normally open (NO) type, these two contacts are connected in series, so the only way to set the output to true is that both contacts I1 and I2 must set to true. For full imagination, please notice the timing diagram of the inputs and output signals shown in Fig. 3. It shows the output is only high when both inputs are high.
Fig. 2: sample ladder logic rung for “AND” logic [2]
Fig. 3: The timing diagram of the “AND” logic gate
AND logic in PLC simulator
- Let us once more enjoy learning further by validating and practicing on the simulator, here you can see in figure 19, on the right the AND logic has been programmed by connecting two switches A and B in series.
- The motor status is the result of the AND logic between the two switches.
- On the left, you can see the results of the simulation by setting the status of switches to simulate all truth table conditions and see the motor status changed accordingly.
- In addition, you can see the truth table of the AND logic on the most right of the figure. So you can review and validate what is going on in the simulator.
Figure 19: Simulating AND logic
The “OR” Logic Gate
This logic gate has two inputs and one output like the “AND” gate. Like its name, the output comes true when either input A or input B comes true as shown in Fig. 4.
Fig. 4: The symbol of “OR” logic gate [1]
Table 2 lists the truth table of the “OR” gate. It lists all possible combinations of inputs and the output status as well. It shows that the output comes to true when input A or input B comes to true.
Table 2: The truth table of the “OR” gate
| Input A |
Input B |
Output |
| False |
False |
False |
| True |
False |
True |
| False |
True |
True |
| True |
True |
True |
Figure 5 shows an example of a ladder logic rung that implements the “OR” logic. We can implement this by connecting two inputs I1 and I2 in parallel branches and to the output. like this way of connection, the output can be set to true by simply setting I1 or I2 or both true. Once more, let us see the timing diagram in fig. 6, it is clearly shown that the output goes high as long as either one or both of the inputs are true.
Fig. 5: sample ladder logic rung for “OR” logic [2]
Fig. 6: the timing diagram of the “OR” logic gate
OR logic in PLC Simulator
- You can see in figure 20, on the right the OR logic has been established and programmed by connecting two switches A and B in parallel.
- The motor status is the result of the OR logic between the two switches.
- On the left, you can see the results of the simulation by setting the status of switches to simulate all truth table conditions of the OR logic and see the motor status charged accordingly.
- In addition, you can see the truth table on the most right of the figure. So you can review and validate what is going on in the simulator.
Figure 20: Simulating OR logic
The “NOT” logic gate
This logic gate has only one input and one output. In a very simple language, the output is the invert logic of the input. So when the input is true, the output would come to false and vise versa as shown in Fig. 7.
Fig. 7: The symbol of the “NOT” logic gate [1]
Table 3 lists the truth table rows of all possible combination of input and output.
Table 3: the truth table of the “NOT” logic gate
| Input |
Output |
| True |
False |
| False |
True |
Figure 8 depicts a very simple example of a ladder logic rung that shows the output Q1 is the reverse logic of the input I1. In addition, Fig. 9 shows the timing diagram of input and output of the “NOT” logic gate. It shows clearly that, the output is the reverse of the input.
Fig. 8: Sample of the ladder logic rung representing “NOT” logic [2]
Fig. 9: The timing diagram of the NOT logic gate
Before going further with the logic gates, I want to let you know the good news that, you can implement any logic by using the aforementioned three logic gates “AND”, “OR”, and “NOT”. However, for simplification, the other logic gates are designed based on using these three logic gates in different topologies to perform a specific logic functions.
Not logic in PLC Simulator
- Also, the NOT logic is one of the primary logic functions, you can see in figure 21, on the right the NOT logic has been designed and programmed by connecting switches A in negative logic in series with the motor.
- The motor status is the result of the NOT logic of switch A. On the left, you can see the results of the simulation by setting the status of the switch to simulate the two-state of the NOT logic truth table and see the motor status charged accordingly.
- In addition, you can see the truth table on the most right of the figure. So you can review and validate what is going on in the simulator.
Figure 21: simulating Not logic
Now! I appreciate your follow-up to our PLC tutorial. I am very happy to feel that, by moving further in our plc tutorial our experience is getting increasing bit by bit. However, some questions may come to our mind like does the operator needs to keep pressing input like the push button to keep the motor running? What happens if he released it, does the motor stop? Well! By asking such questions, I can affirm you start your way to master PLC programming and its logic. And let me say the answer to your questions is yes the operator needs to keep pressing the input push-button until the motor has done its task. But that is not the best practice in the real life. There are other techniques to keep the motor running by one touch of the push button, thanks to latching, setting, and resetting techniques as we will show you in the next sections.
Latching output
- Figure 22 depicts the latching technique that we simply use to keep the motor running by pressing the input push button and having it keep running even after releasing the button.
- As you can see, I have used the Output as a Virtual Input and placed it in parallel with actual input.
Figure 22: Latching output
- Table 2 lists the First three scan cycles to show the sequence of operations and how the latching process works when someone will press the Input.
- In the first scan cycle, when the input gets HIGH, the plc will scan the input "Run (I0.0)" and will find it pressed/ON and thus will make the output "Motor (Q0.0)" HIGH.
- In the second scan cycle, the input "Run (I0.0)" turned off after being released, but the motor contact is still ON from the previous scan cycle.
- So, the compiler won't change the status of the OUTPUT and we can say it's latching the output.
Table 2: The first three scan cycles of latching operation
| Scan cycle |
Run (I0.0) |
Motor status (Q0.0) |
Motor coil (Q0.0) |
| 1 |
1 |
0 |
1 |
| 2 |
0 |
1 |
1 |
| 3 |
0 |
1 |
1 |
- Now let’s add a way to terminate the latching and stop the motor as per request.
- Well! Simply figure 23 shows a stop button is added for terminating the latching condition.
- So in table 2, the RLO for letting the motor running will be unfulfilled by hitting the stop push button in the third scan cycle.
Figure 23: latching with stop button
Simulation of the latching in ladder logic
We may be sure of the logic we wrote for coding the ladder logic of the latching technique. However, at this point how about going to the simulation lab to work out our latch ladder logic program to enjoy validating our ladder code by putting it in the simulator and see how far it match what it is designed for.
Latching Ladder code simulation
- Now let’s try our latching ladder program in the PLCSIM simulator, by entering our ladder logic and starting the simulator.
- Figure 24 shows the first four scan cycles. Notice on the left we can set the inputs on and off and see the effects on the right part.
- In the first scan, every single input and output is at its initial state, so the output is not energized.
- In the next scan cycle, you can notice we switch on input at I0.0 which is the start push button.
- Therefore, the motor has been started and running. In the third scan cycle, the start button is switched back off.
- However, the motor still runs thanks to the latching technique. WOW, we can see our logic is working as we designed for.
- In the last scan cycle, we tried to test stop latching by hitting the stop pushbutton and indeed it stopped latching and the motor stop running.
Figure 24: simulation result of the first ladder program
We will concentrate on moving forward with ladder coding which is our target. However, we just tried to show you at any time you can validate your ladder at any point to enjoy and confirm you are on the right track as long as you are working on your project.
Latching using set and reset
Let’s use another approach for latching which is based on using set and reset coil. Figure 25 shows the set and reset methods.
- By hitting set_valve at address I0.2, the valve at Q0.0 will be set ON until a reset command is present by hitting the reset_valve pushbutton at I0.3.
- It is very easy but you need to take extra care while using set and reset as the last set/reset command will overwrite the previous commands.
- But wait, what’s if an operator keeps pressing the rest or set button for a long time or if the pushbuttons are the stuck and same thing for the stop button.
Well! The rational expectation is that the motor won’t be able to start. However, the good thing is there is a magic solution to differentiate between the situation of this is a normal stop request by the operator or the button is hold pressed unintentionally or due to an issue with the switches. The one-shot technique can magically recognize the event of pressing or releasing the pushbuttons. Therefore, when it is held for a long time or forever that is only one button press event and for triggering it needs to release and pressed once again. That’s amazing but how does it work? Well! Let’s go demonstrate the concept of how it works, implementation using ladder logic, and give an example to understand it consistently and enjoy the magic of one-shot action.
Figure 25: set and reset for easy latching output
The signal edges
Two edges happened when a pushbutton pressed and released which are falling edge and rising edge as shown in figure 26. It depicts the rising edge when the button is pressed and the falling edge when it has been released. Now, let's move to ladder logic, there are two equivalent rising and falling edge contacts that can be used to tell the PLC this is a one-shot signal. Figure 27 shows how the use of the rising edge of the reset pushbutton |P| at address I0.3. it shows that despite the reset being pressed, its effect in the moment of pressing and then it needs to be released and pressed again to reset the valve at Q0.1. in the next section, let’s get to business and work out one practical example which represents a real problem in the industry just to harvest the fruit of what we have learned so far.
Figure 26: The rising and falling edge [2]
Figure 27: The effects of one-shot technique in ladder logic
So, that was all for today. I hope you have enjoyed today's lecture. In the next tutorial, we will simulate Advance Logic Gates using Ladder Logic Programming. We will design NAND, NOR, XOR and XNOR gates in the next lecture. Thanks for reading.
Creating the First Ladder Logic Program in PLC Simulator
Hello friends, I hope you all are doing great. In today's tutorial, I am going to create the first Ladder Logic Program in PLC Simulator. It's 3rd tutorial in our Ladder Logic Programming Series. In our previous tutorial, we have installed PLC Simulator and now we can say our lab is ready to learn and practice. So let us get to work and get familiar with the ladder logic components.
After this article, you will have a complete understanding of PLC contact and coil including their types and possible causes. Because they are the building block of any rung of a ladder logic program. So let us start with ladder logic rung components.
Ladder Logic Contact/Input
- In ladder logic programming, a contact represents the input of the system and it could be a button press by the operator or a signal from the sensor.
- Examples of contacts are toggle switches, pushbuttons, limit switches, sensors like level, pressure, proximity switches et cetera.
- There are two types of contacts normally used, which are:
- Normally Open Contact.
- Normally Closed Contact.
1. Normally Open Contact
- A normally open contact is Open/LOW by default and it gets Closed/HIGH by pressing or getting signal from any external source i.e. sensors.
- As shown in the first row of figure 1, the contact is open or disconnected by default and then the operator turns it to closed or connected status, shown in the second row.
Figure 1: Normally Open (NO) contact [1]
- Let's understand it with its equivalent electrical circuit, imagine you wire a switch in series to a lamp as in figure 2.
- After you complete wiring and connect L1 to the hotline and L2 to the neutral.
- See that at the start the lamp is off until you come and press the pushbutton then it is turned on.
- So, here the switch is acting as a normally open switch.
Figure 2: Normally open contact or switch in a circuit [2]
2. Normally Closed Contact
- A normally closed contact is at HIGH/Closed state by default and gets Low/Open if pressed by the operator.
- Figure 3 shows the symbol of normally close contact.
- So it flows current at the very beginning and disconnects the current flow by being pressed by the operator to become like an open circuit or contact.
Figure 3: Normally Closed (NC) contact
- For elaborating the behavior, let us wire a circuit that is depicted in figure 4.
- The contact is connected in series with a lamp to convey the current and let it turn on.
- So initially, the lamp started in ON status when the contact is not activated by the user.
- And, when the operator activates the contact it turns off.
- So, the switch is acting as a normally closed switch.
Figure 4: Normally close contact or switch in a circuit [2]
Ladder Logic Coil/Output
- The coil in ladder logic represents the actuator or the equipment we aim to turn on or off.
- A good example of a coil is a lamp and motor.
- Typically it is located at the most right side of the ladder logic rung.
- Same as contact has two types based on the initial state and the next state after user activation, also the coil comes in two forms which are:
- Normally Active Coil
- Normally Inactive Coil as shown in figure 5.
- An inactive coil is normally not energized until it gets connected by connecting the left side to the hot wire thanks to a contact.
- In contrast, active or negated coil type comes initially On status or energized and turned off when the left side is connected to the hot wire.
Figure 6: active and inactive coil
Create First Ladder Logic program
To our fortune we no longer need wires and devices to practice what we have been learning together, thanks to the simulator, which we have installed in the previous lecture. Let's create a new project on TIA portal software and test it with the PLCSIM simulator.
Creating a new project on TIA Portal
As this is the first time to use our software to write and simulate a ladder logic code, let us go step by step creating our very first project on the TIA portal software.
- You now get in the Lab by opening the TIA portal and hitting create a new project as shown in Figure 7.
- On the right, you just need to name your project like “new project” and you may leave the default location of projects or alter the data to the project file location as you prefer.
Figure 7: Creating a new project on TIA portal software
- You will have to select a PLC controller whom we are going to use. So you simply select one PLC controller as shown in figure 8 and click okay.
Figure 8: adding PLC controller
- The wizard now goes on asking you to add a program block.
- You can see in Figure 9, the default program block is the Main block which has the main program and other blocks are additional blocks.
- So for now let us go with the essential requirements for our program which is the main block and you just double click on the Main block to go to the next step.
Figure 9: adding program block
I just want to say well done! And congratulate you that you are now all set to start writing your first ladder logic rung as shown in Figure 10. It shows on the left the project components including hardware i.e. devices and controllers, networking devices, configurations, program blocks etc. The most important thing you need to know for now is the program blocks which contain the only main block and other blocks as the project needs. Now! please stare your eye toward the right to see the icon bar that contains every ladder symbol. You can see the contact of normally open and normally closed. Furthermore, you should see the coil and more which we are going to go into detail later in our upcoming articles of PLC tutorial.
Figure 10: starting writing ladder code
Writing First program on the TIA Portal
- WOW! You are a superb learner as I can see you can follow figure 11 and by dragging a contact and dropping it on the blue line, you added a start button of normally open (NO) contact type.
- For identifying contacts and coils, the compiler assigns a unique name & address to each component and can recognize it anywhere in the program.
- Therefore, you just set the address and name for every component you add to your rung.
- The address of components has a specific format that is very logical and easy to understand.
- For example, the contact address “I0.0”, the first character is “I” which denotes input and it is followed by the number of the input module in the rack that holds all inputs and outputs modules.
- Then a number of the input channel as each input module has many channels.
- For instance, an eight channels input module can have numbers from 0 to 7 while 16 channels input module can have numbers from 0 to 15.
- A period is used to separate between the number of input modules and the channel number.
- So by set address I0.0, this refers to the very first channel in the first input module in a PLC rack.
- In addition, a name is used as a tag to easily identify the input i.e. “start” to refer to a start switch.
- Similarly, you add a stop button of the type normally closed (NC) with address I0.1 which means the second input channel in the first input module.
- Furthermore, you double-clicked the coil for the motor and set address Q0.0 which means the first output channel in the first module.
- I know you wonder what is “Q”? Yes! “Q for denoting output like “I” is denoting an input. Well done!. Now let us enjoy simulating the very first code you just have done yourself.
Figure 11: writing the first ladder logic program
Compiling Ladder Logic Program
- Like any programming language, the first thing to do after writing a program is to compile, to make sure it is free of error and ready to be downloaded into the PLC controller to run.
- Figure 12 shows the very simple steps to compile your program by clicking the compile icon in the toolbar which is highlighted in yellow.
- And you can notice in the lowest window below the results of compilation in blue showing that the code is free of error and warnings.
Figure 12: compiling ladder logic program
- To let you imagine how the compiler can help you to find the error location and type, we have done one mistake in the code and compiled as shown in figure 13.
- You can notice that compiler is telling the rung that has the issue which is network 1.
- In addition, the message clarifies the error by telling, you missed the required data for operand which is the address of input is missing.
Figure 13: Example of an error in compilation
Simulating First ladder logic program
- After compiling our program successfully, now the next step is to download it to the PLC controller.
- Yes for sure our simulator will act as the plc controller.
- So, by clicking the simulator button on the toolbar, the simulator window comes out and also another window to download the program to the controller as shown in figure 14.
Figure 14: calling simulator and downloading program
- You simply hit the “start search” button to search for the connected PLC controller.
- In our case, the simulator will appear in the search results.
- So, you just select it and click load to proceed with the wizard of downloading your program as shown in figure 15.
Figure 15: the wizard of downloading the ladder program to plc controller
- By reaching the last screen and clicking finished you have downloaded your first program to the simulator.
- Well done! And let's move forward with simulating our first program to validate our code and enjoy tracing the logic behavior same as a real-time plc controller.
But wait! Will you continue pressing the push button for our motor to keep running? For sure No, there should be a way to let it keep running by just hitting the button thanks to the latching technique.
Simulating our first PLC Program
- After downloading the program and pressing the run button on the very small window of the PLCSIM simulator, we can notice the run/stop indicator turned on in green showing the running status of the PLC as shown in figure 16.
- Now, click on the monitor icon on the toolbar highlighted in yellow on the most right of figure 16, you can notice the rung shows every status of each contact and coil in our program.
- I am very happy to reach this point at which you can see the normally closed contact is showing a green connection as we described above and the normally open contact showing disconnect status and can not wait until the operator press it down to connect and energize the output.
- But how do we press the buttons or switches when we are simulating? There is no physical switches or button to press!!! No friends that are not the case. Let us see how that can happen thanks to the great simulator that we have between our hands.
Figure 16: Simulating the first PLC code
Simulating the operator behavior
- This section is more than exciting, it shows you how the simulator not only does imitate the PLC controller but also it has the facility to imitate devices, switches, push buttons besides showing outputs’ status and values.
- In addition, we will go further in plc programming to show the series and parallel connections of contacts in branches and utilize simple logic AND, OR, NOT to form simple and complicated logics.
- The first way to set inputs on and off is by right-clicking on any contact and modifying the status to 0 or 1 as shown in figure 17.
Figure 17: forcing the inputs on and off
- The other way is to go to the expert mode of the full functional simulator, by hitting the which icon on the very small simulator window.
- A full version of the simulator control window will open up, where you can add inputs and outputs on the right as you can see in figure 18(left side).
- You can notice the inputs have an option in form of a check button to set it on or off.
- As a result, the contact will be turned into the selected status and the program perform according to the new status and the designed logic of your program as shown in figure 18 on the right side.
- It shows the output coil is turned to true status and highlighted in green.
- At this point, I would like to thank you my friends to follow up on our PLC tutorial series and let us move forward to learn further and do more practice with our simulating lab.
Figure 18: operating using simulator full control window
What’s next
Now, how do you see your progress so far? I can see you have just completed the most basics of ladder logic programming. You are now very familiar with the ladder basic components, using the editor to write a ladder logic program, simulate your work for verifying your logic correctness. So you are doing progressively and that’s great to hear that. However, we still have a lot to learn to master ladder logic programming. For example, using blocks like timers, counters, mathematical blocks, data comparison etc. So we hope you have enjoyed what we have reached so far in our PLC tutorial and please get yourself ready for the next part. In the next part, you will learn about types of Timers and how you set their configuration and how you utilize them to achieve the timing-based tasks accurately.