21BLC1133 Esd Exp10
21BLC1133 Esd Exp10
Submitted By
21BLC1133 – S.Pranesh
Submitted To
Dr. Sofana Reka
DATE: 26/03/2024
Slot: L39+L40
Date: 26/03/2024
1
LAB – 10: Working with I2C
AIM:
To understand the Working with I2C.
SOFTWARE REQUIRED: ARM Keil Studio (Mbed Online Compiler), Tera Term
HARDWARE REQUIRED: Micro USB cable, NUCLEO64-STM32L152 Board, LEDs, Jumper Wires (M-F
and M-M), Breadboard,Buzzer, ADXL345.
PROCEDURE:
LAB TASK 1: Read the input from Master and display on Slave
PROGRAM:
Master:
#include "mbed.h"
Serial pc (USBTX, USBRX);
I2C i2c(PB_11, PB_10);
int main(){
int address = 0x50;
char data[20];
pc.printf("Enter data to be sent: \r\n");
2
pc.scanf("%s", &data);
pc.printf("%s", data);
int len = strlen(data);
i2c.write(address, data, len);
wait(2);
}
Slave:
#include "mbed.h"
Serial pc(USBTX, USBRX);
I2CSlave slave(PB_11, PB_10);
int main() {
char buf[20];
char msg[] = "Slave!";
slave.address(0xA0);
while (1)
{
int i = slave.receive();
switch(i) {
case I2CSlave::ReadAddressed:
slave.write(msg, strlen(msg) + 1);
break;
case I2CSlave::WriteGeneral:
slave.read(buf,20);
pc.printf("Read1: %s \r\n", buf);
//wait(1);
break;
case I2CSlave::WriteAddressed:
slave.read(buf,20);
pc.printf("Read2: %s \r\n", buf);
wait(2);
break;
}
3
}
}
OUTPUT:
4
LAB TASK 2: Read Accelerometer axis values and print it in serial monitor.
PROGRAM:
#include "mbed.h"
#include "ADXL345_I2C.h"
ADXL345_I2C accelerometer(PB_11,PB_10);
Serial pc(USBTX, USBRX);
int X_out_raw, Y_out_raw, Z_out_raw; // Outputs int
X_out, Y_out, Z_out; // Outputs
int main() {
int readings[3] = {0, 0, 0};
pc.printf("Starting ADXL345 test...\n");
wait(.001);
pc.printf("Device ID is: 0x%02x\n", accelerometer.getDeviceID());
wait(.001);
// Test whether any of the initialization fails.
if (accelerometer.setPowerControl(0x00)){
pc.printf("didn't intitialize power control\n");
return 0; } wait(.001);
//Full resolution, +/-16g, 4mg/LSB.
if(accelerometer.setDataFormatControl(0x0B)){
pc.printf("didn't set data format\n");
return 0; }
wait(.001);
//3.2kHz data rate.
if(accelerometer.setDataRate(ADXL345_3200HZ)){
pc.printf("didn't set data rate\n");
return 0; }
wait(.001);
//Measurement mode.
if(accelerometer.setPowerControl(MeasurementMode)) {
5
pc.printf("didn't set the power control to measurement\n");
return 0; } while (1) {
wait(0.1);
accelerometer.getOutput(readings);
wait(.1);
X_out_raw = (int16_t)readings[0];
Y_out_raw = (int16_t)readings[1];
Z_out_raw = (int16_t)readings[2];
// Low-pass filter for stable X,Y,Z values
X_out = 0.94 * X_out + 0.06 * X_out_raw;
Y_out = 0.94 * Y_out + 0.06 * Y_out_raw;
Z_out = 0.94 * Z_out + 0.06 * Z_out_raw;
pc.printf("%i,%i,%i\r\n",X_out,Y_out,Z_out); }
}
OUTPUT:
Fig 2: Read
Accelerometer
axis values and
print it in serial
monitor
6
CHALLENGING TASK: Fall detection system using Accelerometer.
PROGRAM:
#include "mbed.h"
#include "ADXL345_I2C.h"
ADXL345_I2C accelerometer(PB_11,PB_10);
Serial pc(USBTX, USBRX);
DigitalOut buzz(PB_8);
7
}
wait(.001);
//Measurement mode.
if(accelerometer.setPowerControl(MeasurementMode)) {
pc.printf("didn't set the power control to measurement\n");
return 0;
}
while (1) {
wait(0.1);
accelerometer.getOutput(readings);
wait(.1);
X_out_raw = (int16_t)readings[0];
Y_out_raw = (int16_t)readings[1];
Z_out_raw = (int16_t)readings[2];
// Low-pass filter for stable X,Y,Z values
X_out = 0.94 * X_out + 0.06 * X_out_raw;
Y_out = 0.94 * Y_out + 0.06 * Y_out_raw;
Z_out = 0.94 * Z_out + 0.06 * Z_out_raw;
pc.printf("%i,%i,%i\r\n",X_out,Y_out,Z_out);
if(X_out > 100 && Y_out > 100){
buzz = 1;
pc.printf("Buzzer ON");
}
else{
pc.printf("Buzzer OFF");
buzz = 0;
}
}
}
OUTPUT:
8
Fig 3: Fall
detection
system using
Accelerometer
OUTPUT:
INFERENCE:
The above program implements the working of Bluetooth and the other peripherals.
RESULT:
9
Thereofore various tasks has been done and we understood the working with I2C in
Nucleo64STM32L152RE Board using Keil Studio Cloud IDE.
10