MEMS sensors or Micro-Electro-Mechanical Systems are the sensors and actuators at the bare metal of embedded systems. MEMS are used for airbag systems, automatic door locks, security systems , earthquake detection, inkjet printers, kitchen appliances, and of course computer peripherals. In this blog we will explore the different ways you can use MEMS sensors. We will also take a look at the elements of a MEMS sensor and how specific MEMS sensors work like accelerometers, gyros and magnetometers as well as some ways that MikroElektronika uses them.
Sensors
Sensors are used for taking mechanical elements of the MEMS sensor and using the electrical elements to process the data produced, and in most cases use Integrated Circuitry ( IC ) to communicate that data with a micro controller. There are many different types of sensors for sensing different types of data such as acceleration, rotational and linear movement, torque, pressure, and temperature sensors. Pressure sensors are used in vehicles to sensor tire pressure, fuel pressure, oil pressure, and the air pressure within the intake manifold of the engine.
Accelerometers
Accelerometers measure acceleration in an x, y and z axis. There are many different ways to measure acceleration in an accelerometer, but the most common approach is by sensing changes in capacitance. When there are two structures near each other there is a measurable electrical capacitance between them. If an accelerometer is moved in any direction the capacitance changes, and after converting this capacitance to a voltage, we can use this as a measurement of acceleration, gyro, etc.
Closeup view of MEMS sensor
The capacitive accelerometer uses micro structures that look like small hair brushes connected to a fixed point. When acceleration is applied to this structure the small fingers of the structure move closer or farther away from each other, changing the electrical capacitance between them. Using circuitry the accelerometer converts this capacitance to a voltage for measurement.
There are many types of accelerometers, but the main difference between accelerometers is the amount of "g" that can be accurately measured. "Low G" accelerometers can measure around 20 g or less and are best suited for low response time, low current consumption, and are usually what is found in smartphones. "High G" accelerometers are designed to detect many more gs, in this case ±100g/±200g/±400g scales can be acheived, and are much more suitable for automotive solutions where the amount of gs is much higher than a smartphone.
Accel 3 Click
Here at MikroElektronika we use accelerometers on click boards like Accel 3 for development uses, as well as in projects such as the Magic Ball click pack to be able to sense which way the board is turned. Here is an example of using our libraries to sense a tap on the accelerometer.
Source code can be found on either Libstock or Github.
#include#include "accel3.h" #include "resources.h" sbit ACCEL_3_CS at GPIOD_ODR.B13; sbit ACCEL_3_INT at GPIOD_IDR.B10; void display_init( void ); void system_setup( uint8_t address, accel_mode_t accel_mode, data_rate_t d_rate, bus_mode_t mode ); void get_calibration( void ); void main() { uint8_t address = 0x18; //I2C Address xyz_t* my_coords; //XYZ values struct system_setup( address, NORMAL, HZ_100, I2C ); get_calibration(); //Calibration is done every time, for accuracy while(1) { my_coords = accel3_get_xyz(); if( my_coords->z_pos > 25 ) //If Accel 3 experiences more than 25g display image { UART1_Write_Text(" Got Big Droprn" ); //Aware the user through UART of drop TFT_Image_Jpeg( 80, 100, hazard_jpg ); //Draw Image Delay_ms(1000); TFT_Set_Pen( CL_WHITE, 1 ); //Erase this portion of screen TFT_Rectangle( 80, 100, 310,215 ); } } }
This code is an example using the EasyMXPro V7 development board and our TFT display to initialize, calibrate, and implement an application using the Accel 3 Click. The first step is to setup our applications' I2C communication, gpio output / inputs, TFT display, UART, and of course the Accel 3 Click.
void system_setup( uint8_t address,accel_mode_t accel_mode, data_rate_t d_rate, bus_mode_t mode ) { //GPIO Outputs GPIO_Digital_Output( &GPIOD_BASE, _GPIO_PINMASK_13 ); GPIO_Digital_Input( &GPIOD_BASE, _GPIO_PINMASK_10 ); Delay_ms(200); //I2C I2C1_Init_Advanced( 100000, &_GPIO_MODULE_I2C1_PB67 ); Delay_ms(200); //Accel 3 Initialization accel3_init( address, accel_mode, d_rate, mode ); //UART Initialization UART1_Init(9600); Delay_ms(200); UART1_Write_Text( "UART Initialized" ); UART1_Write_Text( "rn" ); //TFT Display display_init(); }
The next step is to calibrate the raw 16 bit values that the accelerometer gives us, into a measurment of "g"s.
void get_calibration( void ) { xyz_t* my_coords; int32_t sum[3] = {0}; char* tag[] = {"Z+","Z-","Y+","Y-","X+","X-"}; int16_t dataBuf[6][3] = {0}; char text[70] = { 0 }; char temp_str[3] = { 0 }; uint8_t timer = 0; uint8_t i = 0; int16_t axis_x; int16_t axis_y; int16_t axis_z; TFT_Set_Font( &HandelGothic_BT21x22_Regular, CL_BLACK, FO_HORIZONTAL ); TFT_Write_Text( "Calibration", 20, 60 ); Delay_ms(1000); for( timer = 0; timer < 6; timer++) //Calculate all values pointing towards earths core, and away. { TFT_Set_Pen( CL_WHITE, 1 ); TFT_Rectangle( 100, 124, 310,215 ); TFT_Set_Font( &Verdana12x13_Regular, CL_BLACK, FO_HORIZONTAL ); sprintf( text, "Please Turn " ); strcat( text, tag[timer] ); strcat( text, " up" ); TFT_Write_Text( text, 100, 124 ); Delay_ms(2000); for( i = 0; i < 5; i++) { TFT_Set_Pen( CL_WHITE, 1 ); TFT_Rectangle( 100, 148, 310,148 + 15 ); sprintf( text, "Start will be in %d", ( 5 - i ) ); strcat( text, " seconds" ); TFT_Write_Text( text, 100, 148 ); Delay_ms(1000); } TFT_Write_Text( "Calculating...", 100, 172 ); for( i = 0 ; i < 100; i++) { my_coords = accel3_get_xyz_raw(); sum[0] += my_coords->x_pos; sum[1] += my_coords->y_pos; sum[2] += my_coords->z_pos; } Delay_ms(20); dataBuf[timer][0] = sum[0]/100; dataBuf[timer][1] = sum[1]/100; dataBuf[timer][2] = sum[2]/100; sum[0] = sum[1] = sum[2] = 0; TFT_Write_Text( "Calculate Success", 100, 196 ); Delay_ms(1000); } TFT_Set_Pen( CL_WHITE, 1 ); TFT_Rectangle( 100, 124, 310,196 ); axis_x = (dataBuf[4][0] + dataBuf[5][0])*0.3 + (dataBuf[0][0]+dataBuf[1][0]+dataBuf[2][0]+dataBuf[3][0])/10; sprintf( text, "Adjust Value of X is %ul", axis_x ); TFT_Write_Text( text, 100, 124 ); axis_y = (dataBuf[2][1] + dataBuf[3][1])*0.3 + (dataBuf[0][1]+dataBuf[1][1]+dataBuf[4][1]+dataBuf[5][1])/10; sprintf( text, "Adjust Value of Y is %ul", axis_y ); TFT_Write_Text( text, 100, 148 ); axis_z = (dataBuf[0][2] + dataBuf[1][2])*0.3 + (dataBuf[2][2]+dataBuf[3][2]+dataBuf[4][2]+dataBuf[5][2])/10; sprintf( text, "Adjust Value of Z is %ul", axis_z ); TFT_Write_Text( text, 100, 172 ); Delay_ms(3000); set_x( axis_x ); set_y( axis_y ); set_z( axis_z ); TFT_Set_Pen( CL_WHITE, 1 ); TFT_Rectangle( 20, 60, 310,215 ); TFT_Set_Font( &HandelGothic_BT21x22_Regular, CL_BLACK, FO_HORIZONTAL ); TFT_Write_Text( "Drop Test", 20, 60 ); }
In this calibration, we are finding the sensitivity of our sensor. This can be determined by applying 1 g acceleration to each axis. This is done by pointing the axis towards the center of the Earth, taking its' value and storing it, rotating the sensor 180 degrees and then storing that value as well. In this example, we take many values of each axis pointing towards the center of the Earth and away, so that we have an average value to work with. By subtracting the larger output from the smaller value, and dividing the result by 2, this is how we find the sensitivity of the sensor.
while(1) { my_coords = accel3_get_xyz(); if( my_coords->z_pos > 25 ) //If Accel 3 experiences more than 25g display image { UART1_Write_Text(" Got Big Droprn" ); //Aware the user through UART of drop TFT_Image_Jpeg( 80, 100, hazard_jpg ); //Draw Image Delay_ms(1000); TFT_Set_Pen( CL_WHITE, 1 ); //Erase this portion of screen TFT_Rectangle( 80, 100, 310,215 ); } }
Finally, we have our while loop. This is where we get our x, y and z values from the accelerometer, and check to see if our z axis has experienced more than 25 gs ( drop from a high distance ), and alerts the user with a hazard image. Great example right? Don't go dropping your development board, this is just a test.
Digital Gyros
To help understand how a gyro works let's look at a plane and the different rotations that can happen on all three different axes. yaw( z-axis ), pitch( y-axis ), and roll( x-axis ). If the plane is to turn left or right, it would simply rotate on the z axis, and if the plane wanted to rotate turning one wing of the plane toward the sky and the other towards the earth, it would rotate on the x axis. Finally, if the plane wanted to increase or decrease its' altitude it would rotate on the y axis.
Basically, MEMS gyroscopes are designed to detect rotation on any of the three axes described above. We use the data output from this sensor to prevent rolling over in modern automobiles, keep drones level in the sky, or even play your favorite games on your phone or portable gaming devices. MikroElektronika has click boards such as the Gyro Click, for sensing rotations or our 3D Motion Click which uses a 3-axis accelerometer, a gyroscope, a magnetometer, and a motion co-processor to provide reliable and accurate positioning and orientation information for your projects needs.
Compasses
Another great MEMS sensor is the magnetometer. Magnetometers use coils on a different bar for each axis to detect the magnetic field in each axis ( x, y, z ). Based on Lorentz force, when one of these bars experience a strong magnetic force, the current passing through the bar will cause it to bend. The magnetometer can then use that calculation and send it to the micro controller for determining the direction of north. At MikroElektronika we have a Compass Click that uses a IC interface capable of measuring both linear acceleration and magnetic field. Using these sensors we are able to use this click board for compasses, position detection, map rotation, pedometer, display orientation and many more.
Summary
MEMS sensors are a fundamental part of what makes the technology of tomorrow seem smart, and also helps us monitor things that processors and clever software would never be able to do on their own.
References
Libstock libraries for Accel 3 Click 2016
Manual for Accel 3 Click 2016
Accelerometer Wiki Page 2016
Magnetometers Engineers Garage 2012
Gyros Info 2016
Lorentz force Wiki Page 2016