CPS Lab Report Final
CPS Lab Report Final
CALICUT
Department of Electronics and Communication Engineering
Winter 2023
SUBMITTED BY
M231007EC Srinivas k
M230843EC Rishab SR
Group 13 - EDT
M.Tech - Electronics Design Technology
1. DC Motor Step Response
AIM : To Design and Simulate a DC motor model using MATLAB SIMULINK Software.
INTRODUCTION : the model circuit of dc motor with the armature is shown below & the
mechanical rotational equipment’s is also shown which indicates the dynamics experienced
by the rotor such as inertia(J) , friction constant (B).
Circuit parameter details :
R: indicated the armature winding cupper loss and conduction parameter
L: The internal power flow of the DC motor is alternating current(AC) so when the AC
current flow in to the winding wire the inductance creeps in which is modelled as L
So, Over all it forms a series circuit with the back emf (rotor) and the applied Vdc is
converted to AC by commutator which is known as mechanical rectifier
Load model is modelled with J,B,θ Which is force voltage analogy and we use force free
body diagram equation knowns as d'Alembert's principle instead of Kirchhoff's laws
The mechanical component in the model integrates the angular acceleration to calculate the angular speed of the
motor and load, which the electrical system uses to calculate the current.
The motor in this model has a resistance(R) of 3Ω, an electrical constant(Ke) of 0.025 Vs/rad, and a torque
constant(b) of 0.25 Nm/A. The total moment of inertia for the system(J) is 3.2μNms2, resulting in a
mechanical time constant(Kt) of 15.4ms.
The motor in this model has an inductance(L) of 2.5μH and a resistance(R) of 3Ω, resulting in an electrical
time constant (te) of 200ns.
SCHEMATIC DIAGRAM :
PROCEDURE :
1) open Simulink in MATLAB tool, then go to the library browser & add the summer block , integrator
controller(1/s) ,proportional controller (k)
2) we consider the step input as R(s) input side and scope is connected at the output side as C(s) since
the model equations are derived from the transfer function
3) DC_MOTOR = C(S) R(S) = K (𝐽(𝑆+𝑏)∗𝐿(𝑆+𝑅)+K2) all these parameters are modelled as block
diagram.
4) Now consider the step response mentioning the wave form timing details as.
Parameter Value
Initial 0V
Final 230V
RESULTS :
Fig 1.5 Waveform window of Angular Position
Objective: The main objective of this experiment is to understand the basics of GPIOs, how
a pin can be configured as an output or an input pin etc. By understanding these, we will
configure the PA 5 pin of the microcontroller as a GPIO Output pin, because the LD2, which
is in-built on the microcontroller, is connected to this pin. Then with the help of code we will
change the state of the LED. In this experiment, we're asked to make the LED turn ON and
OFF using the GPIO.
Requirements:
STM32 Cube IDE software.
STM32 Microcontroller.
USB Cable for the microcontroller.
PC or Laptop.
Steps followed :
1. Create a New Project in STM32.
In the Pinout & Configuration tab, click on PA5 pin and select it as GPIO_OUTPUT, if it is
not selected by default.
5. Press Ctrl+S to generate your code.
CODE :
1. Insert this code snippets in the main.c code.
2. This code must be included in while loop for toggling of LED.
}
/* USER CODE END WHILE */
Output :
Fig 2.1 LED toggling (ON State) Fig 2.2 LED toggling (OFF State)
Overview: GPIO is a very important peripheral for interfacing digital IO devices. Here,
learns the technique to configure the GPIO peripheral for basic devices such as onboard
LEDs and Buttons. Also learn how to configure the GPIO interrupts functionality.
Steps followed :
1. Create a New Project in STM32 .
2. Select the Board in the Board Selector as NUCLEO L476RG .
CODE :
1. Insert this code snippets in the main.c code.
2. This code must be included in while loop for toggling of LED.
3. Insert the following code snippet to turn on the LED when the switch is pressed else
LED will be in OFF state.
Output :
Fig 2.3 When the switch is Not pressed: Fig 2.4 When the switch is pressed
3. Interface BH1750 and audio sensor to STM32 board
Overview: This experiment features the interfacing of light sensor to STM32 board to collect
sample data. BH1750 is a Digital Ambient light sensor, as it uses the I2C communication
protocol. Here, we can measure the intensity of ambient light in its surroundings. It is used to
detect the level of brightness or illuminance.
Steps followed :
4. Make all the necessary pinout adjustments as instructed in the DigiToad Technology
Instruction Manual.
Code :
#include "main.h"
#include "bh1750.h"
#include "stdio.h" #include
"string.h"
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_I2C1_Init();
BH1750_Init(&hi2c1);
while (1)
{
/* USER CODE END WHILE */
float my_lux = BH1750_ReadLux(&hi2c1);
printf("Lux = %.2f\r\n", my_lux); // @suppress("Float formattingsupport")
HAL_Delay(1000);
/* USER CODE BEGIN 3 */
}
Output :
Various intensity values were sensed and displayed in the command window.
Steps followed :
1. Create a New Project in STM32 .
Code :
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
Log();
/* USER CODE END WHILE */
Output :
Create a Command shell console to print the contents on the console.
Observe the output in the serial port connection in the command shell console.
4. Configuration of UART in polling method to send data from STM32 MCU to PC
Overview: This experiment features the Serial Communication peripheral also known as
UART. This lab forms the basis of designing dataloggers. Here, learns the technique to send
text or numeric data from the STM32 board to the PC.
Procedure: This experiment features the Serial Communication peripheral also known as UART. This
lab forms the basis of designing dataloggers. Here, we learn the technique to send text or numeric
data from the STM32 board to the PC.
Steps followed :
4. Make all the necessary pinout adjustments as instructed in the DigiToad Technology
Instruction Manual.
Code :
// Type in all the below code snippets in your main.c file
#include "main.h"
UART_HandleTypeDef huart2;
int main(void)
{
HAL_Init();
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
if(HAL_UART_Transmit(&huart2, " HELLO ",7,100) != HAL_OK)
{
Error_Handler();
HAL_Delay(100);
}
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
Output :
Create a Command shell console to print the contents on the console.
Set the connection Type to be Serial Port.
Give the connection name as uart.
Observe the output in the serial port connection in the command shell console.
5. Design of a coherent sensor signal sampling methodology to implement a data logger
to send audio sensor data from STM32 to NanoEdge AI Studio on PC
Overview: This is a follow up experiment from the previous lab. Here, we design an audio
datalogger. This datalogger can collect real time audio samples and store it into a PC as a
CSV file. The audio csv files are later used for classification applications.
Instructions : To design an audio datalogger. This data logger can collect real-time audio samples and
store it into a PC as a CSV file. The audio CSV files will be later used for classification applications.
Procedure:
1. Open Nano Edge AI and create a New project.
2. In the Next step add the signals and name them as Train horn , baby cry and ambulance .
3. Take 100 samples of each audio you are trying to train the model for further classification,
4. In the next step go to Bench mark . Run the new Bench mark. Run the Bench Mark such that the
score will be greater than 90% .
5. In the next step go to the Emulator stage and initialize the emulator with different signals which
are added previously.
Output :
Fig 5.1 Audio data logging of Baby Cry Fig 5.2 Audio data logging of Ambulance
6. Audio Recognition: Design of keyword recognition (ON & OFF) model to control the
status of an LED using voice commands
Overview: This experiment deals with the implementation of machine learning to detect two
keywords from the audio samples. These keywords are: ON & OFF, so, if the user utters the
word ON, the corresponding LED will be turned on and vice versa.
Objective :
The main objective of this experiment is to interface an audio sensor to
an STM32 microcontroller and deploy the machine learning model built
using the nanoedge AI studio into the microcontroller .this will give the
microcontroller the ability to make decision on the device itself based on
classification on real time audio sensor data
Circuit diagram:
Requirements :
1. STM32 Cube IDE software.
2. Audio Sensor (Analog).
3. STM32 Microcontroller.
4. USB Cable for the microcontroller.
5. Jumper Wires.
6. PC or Laptop.
Data classification Code :
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
TIM_HandleTypeDef htim2;
UART_HandleTypeDef huart2;
if(buffer_index>=DATA_INPUT_USER){
buffer_index=0;
}
}
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
enum neai_state error_code;
/* USER CODE BEGIN Init */
error_code = neai_classification_init(knowledge);
if(error_code !=NEAI_OK)
{
printf("Knowledge inititalization ERROR");
printf("%d",error_code);
}
else
{
printf("Knowledge initialization done");
}
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
Inference();
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/**
* @brief ADC1 Initialization Function
* @param None
* @retval None
*/
static void MX_ADC1_Init(void)
{
/**
* @brief TIM2 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM2_Init(void)
{
/**
* @brief USART2 Initialization Function
* @param None
* @retval None
*/
static void MX_USART2_UART_Init(void)
{
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
void Inference()
{
uint16_t i,id_class_t0,id_class_tn;
fill_mic_buffer();
neai_classification(mic_buffer,output_class_buffer,&id_class_t0);
for(i=0;i<CONFIRMATIONS_NB-1;i++)
{
fill_mic_buffer();
neai_classification(mic_buffer,output_class_buffer,&id_class_tn);
if(id_class_t0!=id_class_tn)
{
break;
}
if(id_class_t0 ==id_class_tn)
{
printf("Detected Class");
printf(id2class[id_class_t0]);
// printf("i=%d \n",i);
// printf("t0=%d\n",id_class_t0 );
// printf("tn = %d \n",id_class_tn);
if(id_class_tn == 1 ){
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_SET);
//printf("police siren");
}
else
{
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_RESET);
}
printf("\r\n");
}
else
{
printf("?");
printf("\r\n");
}
}
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
7. Street Light Control using Light Sensor (BH1750)
Overview: Street lighting control is crucial for urban areas to optimize energy consumption
and ensure adequate lighting for pedestrians and vehicles. Implementing BH1750 on the
STM32 to adjust streetlight brightness based on real-time light intensity and time of day.
Steps followed :
4. Make all the necessary pinout adjustments as instructed in the DigiToad Technology
Instruction Manual.
Code :
Include all the below code snippets inside you main.c file.
#include "main.h"
/* USER CODE BEGIN Includes */
#include "bh1750.h"
#include "stdio.h" #include
"string.h"
/* USER CODE END Includes */
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
BH1750_Init(&hi2c1);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
Output :