0% found this document useful (0 votes)
5 views33 pages

Lab 6 Report

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 33

Faculty of Engineering Technology Page No.

1 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE

FACULTY OF ENGINEERING TECHNOLOGY


DEPARTMENT OF ELECTRICAL ENGINEERING TECHNOLOGY

PG-N-MM
(MICROCONTROLLER LABORATORY)

WORKING INSTRUCTION AND REPORT

Course Code & Name /


BNR 21703 (MICROPROCESSOR AND MICROCONTROLLER)
Kod & Nama Kursus
Code & Title of Experiment/ EXP06_GETTING STARTED WITH STM32F334C8 AND ARDUINO
Kod & Tajuk Ujikaji IDE
Date of Experiment/
20/5/2024
Tarikh Ujikaji
Programme/Program BNF
Group/ Kumpulan GROUP 9
Matric No./
Name/Nama
No. Matrik
1. MUHAMMAD DINI BIN NIZAM CN220191
Group Members/
2. AHMAD LUQMAN BIN KARIM AN220043
Ahli Kumpulan
3. MUHAMMAD SYAMRIN SYAMIR BIN
AN220058
MOHD SAIDI
4.
5.
Instructor Name / 1. DR. NORAIMI SYAMIMI BINTI AB GHAFAR
Nama Instruktor 2. DR NOR AKMAL BINTI ALIAS
Results and Data Analysis /
/ 70 %
Keputusan dan Analisis Data
Question / Soalan / 10 %
Equipment Handling /
Assessment / Penilaian / 05 %
Penggunaan Alatan
Team Work / Kerja Berkumpulan / 10 %
Conclusion / Kesimpulan / 05 %
TOTAL / JUMLAH / 100 %
Faculty of Engineering Technology Page No. 2 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE

1. EXPERIMENT LEARNING OUTCOMES

At the end of the experiment, students should be able to:

1.1 Be familiar with the basic operation and specification of STM32 Board and be familiar with the
functionalities of Integrated Development Environment (IDE).
1.2 Know how to program the STM32 board to execute desired tasks.

2. INTRODUCTION / THEORY

2.1. STM32F334C8 Discovery Board

Arduino is a tiny computer that you can program to process inputs and outputs going to and
from the chip. The Arduino is what is known as a Physical or Embedded Computing platform,
which means that it is an interactive system that through the use of hardware and software
can interact with its environment.

The STM32F334 discovery kit helps you to discover the digital power features of the
STM32F334 and to develop your applications easily.

Figure 1: STM32F334C8 Discovery Board

This board is based on an STM32F334C8T6. It includes an ST-LINK/V2-1 embedded debug tool


interface, high brightness LED dimming with buck converter, buck/boost converter, LEDs and
pushbuttons.
Faculty of Engineering Technology Page No. 3 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE
To support quick evaluation and development, a large number of free ready-to-run
application firmware examples are available at www.st.com/stm32f3-discovery.

Microcontroller features
• STM32F334C8T6 in LQFP48 package
• ARM®32-bit Cortex®-M4 with FPU
• 72 MHz max CPU frequency
• VDD from 2.0V to 3.6 V
• 64 KB Flash
• 16 KB SRAM
• GPIOs (37) with external interrupt capability
• 12-bit ADCs with 15 channels (2)
• 12-bit DAC channels (3)
• USARTs (3)
• I2C (1)
• SPI (1)
• High-resolution Timer (1)
• Other Timers (8)
• Watchdog Timers (2)
• Ultra-fast Analog Comparators (3)
• Operational Amplifiers (1)
• CAN interface 2.0 B Active (1)

Board features
• On-board ST-LINK/V2-1 debugger/programmer with SWD connector
o Selection-mode switch to use the kit as a standalone ST-LINK/V2-1
• Flexible board power supply
o USB VBUS or external source (5 V)
o Power management access point
• High brightness LED dimming with buck converter
• Four user LEDs: LD3 (red), LD4 (orange), LD5 (green) and LD6 (blue).
• Two push buttons: USER and RESET
o Virtual Com port
o Mass storage (USB Disk drive) for drag'n'drop programming
o Debug port
Faculty of Engineering Technology Page No. 4 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE
Board pinout

Figure 2: STM32F334C8 Board Pinout


Faculty of Engineering Technology Page No. 5 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE

2.2. Integrated Development Environment (IDE)

The Arduino IDE 2 features a new sidebar, making the most commonly used tools more
accessible.

Figure 3: Arduino IDE 2

• Verify / Upload - compile and upload your code to your Arduino Board.
• Select Board & Port - detected Arduino boards automatically show up here, along with the port
number.
• Sketchbook - here you will find all of your sketches locally stored on your computer. Additionally,
you can sync with the Arduino Cloud, and also obtain your sketches from the online environment.
• Boards Manager - browse through Arduino & third party packages that can be installed. For
example, using a MKR WiFi 1010 board requires the
Arduino SAMD Boards
package installed.
• Library Manager - browse through thousands of Arduino libraries, made by Arduino & its
community.
• Debugger - test and debug programs in real time.
• Search - search for keywords in your code.
• Open Serial Monitor - opens the Serial Monitor tool, as a new tab in the console.

The Arduino IDE 2 is a versatile editor with many features. You can install libraries directly, sync
your sketches with Arduino Cloud, debug your sketches and much more.
Faculty of Engineering Technology Page No. 6 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE

Table 1: Common Arduino Programming Command


Components / Pins Functions
Configures the specified pin to behave either as an input or an output. See
pinMode(pin, Mode)
the reference page below
digitalWrite(pin, value) Ouputs either HIGH or LOW at a specified pin
digitalRead(pin) Reads the value from a specified pin, it will be either HIGH or LOW
int analogRead(pin) Reads the value from the specified analog pin.
analogWrite(pin, value) Writes an analog value (PWM wave) to a pin.
Pauses your program for the amount of time (in miliseconds) specified as
delay(ms)
parameter
Sets the data rate in bits per second (baud) for serial data transmission.
For communicating with the computer, use one of these rates: 300, 1200,
Serial.begin(int speed) 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200. You
can, however, specify other rates – for example, to communicate over
pins 0 and 1 with a component that requires a particular baud rate
Get the number of bytes (characters) available for reading over the serial
int Serial.available()
port
int Serial.read() Reads incoming serial data
Serial.print(data) Prints data to the serial port

3. EQUIPMENT

3.1. STM32F334C8 Discovery Board


3.2. Arduino IDE
3.3. USB Mini-B Cable

4. PRELIMINARIES

4.1. Arduino IDE Installation


4.2. Arduino IDE Installation

Requirements
• Windows - Win 10 and newer, 64 bits
• Linux - 64 bits
• macOS - Version 10.15: "Catalina" or newer, 64 bits
Faculty of Engineering Technology Page No. 7 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE
The Arduino IDE 2 is an open-source project. It is a big step from its sturdy predecessor,
Arduino IDE 1.x, and comes with revamped UI, improved board & library manager,
debugger, autocomplete feature and much more.

Download the Editor

(i) Downloading the Arduino IDE 2 is done through the Arduino Software page. Here you will
also find information on the other editors available to use.

Installation – Windows

(ii) To install the Arduino IDE 2 on a Windows computer, simply run the file downloaded from
the software page.

(iii) Running the installation file.


(iv) Follow the instructions in the installation guide. The installation may take several minutes.
Faculty of Engineering Technology Page No. 8 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE

(v) You can now use the Arduino IDE 2 on your Windows computer!

Installation – macOS

(vi) To install the Arduino IDE 2 on a macOS computer, simply copy the downloaded file into
your application folder.

(vii) You can now use the Arduino IDE 2 on your macOS computer!
Faculty of Engineering Technology Page No. 9 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE

4.3. STM32 Cube Programmer Installation

(i) Run the STM32CubeProgrammer Installer

(ii) Click next


Faculty of Engineering Technology Page No. 10 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE

(iii) Select default installation path


Faculty of Engineering Technology Page No. 11 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE

(iv) Click OK

(v) Click next


Faculty of Engineering Technology Page No. 12 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE

(vi) Click next


Faculty of Engineering Technology Page No. 13 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE

(vii) Click Install

(viii) Click Finish


Faculty of Engineering Technology Page No. 14 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE

(ix) Click next


Faculty of Engineering Technology Page No. 15 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE

(x) Connect your board via USB cable. Launch STM32CubeProgrammer


Faculty of Engineering Technology Page No. 16 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE

(xi) Click firmware upgrade

(xii) Click open in update mode


Faculty of Engineering Technology Page No. 17 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE

(xiii) Click upgrade

(xiv) Upgrade is successful


Faculty of Engineering Technology Page No. 18 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE

(xv) Click refresh, and connect

(xvi) Device has been successfully connected to STM32CubeProgrammer


Faculty of Engineering Technology Page No. 19 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE

(xvii) Click disconnect


Faculty of Engineering Technology Page No. 20 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE

5. PROCEDURES

5.1. Task 1: LED Blinking

(i) To make your STM32 device compatible with Arduino’s IDE, we need to install the STM32
support. This can be achieved by clicking: Arduino IDE > File > Preferences:

(ii) In the Preferences dialog window, add the following URL to the "Additional Boards
Managers URLs" field, then click OK:

https://github.com/stm32duino/BoardManagerFiles/raw/main/pack
age_stmicroelectronics_index.json
Faculty of Engineering Technology Page No. 21 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE

(iii) Now, click on Tools > Board > Boards Manager:

(iv) Select “Contributed” type, search for "STM32 MCU based boards" and install:
Faculty of Engineering Technology Page No. 22 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE
(v) After the installation is complete, close the board manager and go to Tools > Board
> STM32 MCU based boards.
(vi) A list of STM32 MCUs appears, so you can choose between Nucleo, Discovery and
Evaluation boards, or generic STM32 MCUs.
(vii) By selecting the specific board type, you are able to choose the specific board part
number for your board later on.
(viii) By choosing the Generic STM32F3 Series option, later you are able to select a generic
microcontroller for the selected family.

(ix) Great! Your board is now correctly selected and recognized by the Arduino IDE!
(x) Let us start programming our STM32 device with a simple Blink LED through the Arduino’s
IDE.
(xi) To perform this task, we have a few options. These options include programming
the STM32 MCU through the Serial Wire Debug, using an STLINK device (external
or embedded), and also through bootloader serial port, DFU etc.
(xii) Let us see below how to program our STM32 MCU using Arduino’s IDE to get it
working properly as expected:
(xiii) Having the STM32CubeProgrammer installed helps program the STM32 Nucleo,
Discovery or Eval board through SWD, Serial or DFU methods with ease. The
STM32CubeProgrammer enables drag-and-drop flash programming, so there is no
need for a separate debug probe. However, this step is optional and you can
program directly via Arduino's IDE as well.
(xiv) Go to Arduino IDE, and go to File > New Sketch.
Faculty of Engineering Technology Page No. 23 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE
(xv) Please copy the coding below. Save the “Sketch” as Lab6_Task1 in the subfolder “Lab
MicroC” created.

/*
Blink

Turns an LEDs on for one second, then off for one second, repeatedly.

*/

// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(PB6, OUTPUT);
pinMode(PB7, OUTPUT);
pinMode(PB8, OUTPUT);
pinMode(PB9, OUTPUT);
}

// the loop function runs over and over again forever


void loop() {
digitalWrite(PB6, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(PB7, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(PB8, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(PB9, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(PB6, LOW); // turn the LED off by making the voltage LOW
digitalWrite(PB7, LOW); // turn the LED off by making the voltage LOW
digitalWrite(PB8, LOW); // turn the LED off by making the voltage LOW
digitalWrite(PB9, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Faculty of Engineering Technology Page No. 24 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE

(xvi) Go to Tools > Board Part Number > Generic F334C8Tx. Leave other settings as default.

(xvii) With the microcontroller correctly selected and the board properly connected, let us define
the COM Port where the device is connected. Go to Tools > Ports > Select the Port Number:

(xviii) Click the Upload Button, wait for the code to be compiled and the board to be programmed:
Faculty of Engineering Technology Page No. 25 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE

Please discuss and draw the programming flow chart for Task 1:
Faculty of Engineering Technology Page No. 26 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE

Please show the picture of the output from Task 1:

Discuss your observation on Task 1 output:


In this task we can observe that if the user press the button, all the LED PB6, PB7, PB8 and PB9 will turn
on with delay of 1 second. This process will repeat over and over again in a loop.
Faculty of Engineering Technology Page No. 27 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE
5.2. Task 2: Reading Push Button Input to Trigger LED Outputs

(i) Copy the coding below. Save the “Sketch” as Lab6_Task2 in the subfolder “Lab MicroC”
created.

/*
Button_LEDs_STM32
This code is designed to read USER button and turn ON LEDs for 5 seconds when the button
is pressed.
*/

int buttonState = 0;

// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(PA0, INPUT);
pinMode(PB6, OUTPUT);
pinMode(PB7, OUTPUT);
pinMode(PB8, OUTPUT);
pinMode(PB9, OUTPUT);
}

// the loop function runs over and over again forever


void loop() {

buttonState = digitalRead(PA0);

if(buttonState == HIGH){
digitalWrite(PB6, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(PB7, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(PB8, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(PB9, HIGH); // turn the LED on (HIGH is the voltage level)
delay(5000); // wait for 5 seconds
}
else{
digitalWrite(PB6, LOW); // turn the LED off by making the voltage LOW
digitalWrite(PB7, LOW); // turn the LED off by making the voltage LOW
digitalWrite(PB8, LOW); // turn the LED off by making the voltage LOW
digitalWrite(PB9, LOW); // turn the LED off by making the voltage LOW
delay(5000); // wait for 5 seconds
}
}
Faculty of Engineering Technology Page No. 28 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE

(ii) Click the Upload Button, wait for the code to be compiled and the board to be programmed.
(iii) Observe the output. Try and press the user button (blue button). What happened? Does the
LEDs light up as expected? Discuss the reason why the button is not responsive.

Please discuss and draw the programming flow chart for Task 2:
Faculty of Engineering Technology Page No. 29 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE
Please show the picture of the output from Task 2:

Discuss your observation on Task 2 outputs:


Task 2 shows that the code is designed to read USER button and turn ON LEDs for 5 seconds when the
button is pressed. The button state is the output and set the PA0 as the input. So, the program will read
the button state and turn on all LEDs with delay 5 seconds or else the LEDs will turn off, this process will
repeated in a loop.
Faculty of Engineering Technology Page No. 30 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE
(iv) Edit the Lab6_Task2 code to solve the problem of irresponsive user button that you
discovered. In addition, instead of turning on all LEDs at once when the button is pressed,
you are required to create a unique sequence in turning on the LEDs. Save your edited
program code as “Lab6_Task2b”.

Please discuss and draw the programming flow chart for Task 2b:
Faculty of Engineering Technology Page No. 31 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE

Please show the picture of the output from Task 2b:

Discuss your observation on Task 2b outputs:


In this task we observe that it set the button state as 0 and read the pin PA0 then save into button state.
If the button state reads as 1 it will turn on all LEDs with 1 second delay in each LEDs and the process
repeats as loop.
Faculty of Engineering Technology Page No. 32 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE

Attached the program code for Task 2b:

/*
Button_LEDs_STM32
This code is designed to read USER button and turn ON LEDs for 5 seconds when the button is
pressed.
*/

int buttonState = 0;

// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(PA0, INPUT);
pinMode(PB6, OUTPUT);
pinMode(PB7, OUTPUT);
pinMode(PB8, OUTPUT);
pinMode(PB9, OUTPUT);
}

// the loop function runs over and over again forever


void loop() {

buttonState = digitalRead(PA0);

if(buttonState == HIGH){
digitalWrite(PB6, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(PB7, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(PB8, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(PB9, HIGH); // turn the LED on (HIGH is the voltage level)
delay(5000); // wait for 5 seconds
}
else{
digitalWrite(PB6, LOW); // turn the LED off by making the voltage LOW
digitalWrite(PB7, LOW); // turn the LED off by making the voltage LOW
digitalWrite(PB8, LOW); // turn the LED off by making the voltage LOW
digitalWrite(PB9, LOW); // turn the LED off by making the voltage LOW
}
}
Faculty of Engineering Technology Page No. 33 / 33

Department of Electrical Engineering Technology Revision No. 1


Title: EXP06_GETTING STARTED WITH
Effective Date 18/05/2024
STM32F334C8 AND ARDUINO IDE

5. CONCLUSION

This study of the STM32F334C8 microcontroller and Arduino IDE seamlessly merges the power of the
STM32 platform with the user-friendly Arduino environment, offering users a robust foundation for
advanced embedded systems projects. For example we can make the LEDs turn on and off in various type
of combination for instance make the traffic light LED and more. This integration lowers the entry barrier,
empowering enthusiasts and professionals to focus on creativity and innovation, making it an ideal for
students to explore embedded system development

Prepared by / Disediakan oleh : Approved by / Disahkan oleh :

Signature / Tandatangan : Aimi Syamimi Signature / Tandatangan : Lam Hong Yin


Name / Nama : Dr. Aimi Syamimi binti Ab Ghafar Name / Nama : Ts. Dr. Lam Hong Yin
Date / Tarikh : 18 May 2024 Date / Tarikh : 18 May 2024

You might also like