Create OLED Display Block to Display Acceleration and Tap Count Using IO Device Builder App
This example shows how to use the IO Device Builder app to create OLED Display block to measure acceleration, tap count and then display the same in OLED (Organic Light Emitting Diodes) display module.
OLED is a flat light-emitting technology made by placing a series of organic thin films between two conductors. When an electrical current is applied, a bright light is emitted. In this example, the values are displayed using the I2C protocol requiring two wires (SCL and SDA).
Note: By default, communication libraries such as SPI, I2C, and Servo are included. For information on adding any other libraries, see Working with Arduino Libraries in IO Device Builder.
Prerequisites
Download the Adafruit_SH1106 drivers from GitHub®.
Download the dependent file Adafruit_BusIO from GitHub.
Download the dependent file Adafruit_GFX_Library from GitHub.
Circuit Connections
To run this example, make these circuit connections.
Connect Arduino VCC to OLED Module VCC.
Connect Arduino GND to OLED Module GND.
Connect SCL to SCL and SDA to SDA pin from the sensor to Arduino®.
Create ADXL343 Sensor Block and OLED Display Block
Create the ADXL343 Sensor block using the steps listed in Create ADXL343 Sensor Block to Read Acceleration Using IO Device Builder App. While performing the steps listed, in the Select Outputs for the Block section, add an additional output port, TapDetection
with data type as uint16
.
After creating the ADXL343 Sensor block, modify the generated .cpp
file as shown below.
#include "C:\Users\Device_Libraries\ADXL Drivers\appTest4\adxlT4.h" #include "Wire.h" #include "Adafruit_Sensor.h" #include "Adafruit_ADXL343.h" #include "Arduino.h" #define ADXL343_CS 10 Adafruit_ADXL343 accel = Adafruit_ADXL343(ADXL343_CS, &SPI, 12345); #define INPUT_PIN_INT1 (5) int_config g_int_config_enabled = { 0 }; int_config g_int_config_map = { 0 }; volatile int count=0; void int1_isr(void) { count=count+1; } void setupFunctionadxlT4(int8_T * accelRange,int size_vector__1){ while (!Serial); if(!accel.begin()) { /* Checking the connections */ while(1); } accel.setRange(*accelRange); pinMode(INPUT_PIN_INT1, INPUT); attachInterrupt(digitalPinToInterrupt(INPUT_PIN_INT1), int1_isr, RISING); g_int_config_enabled.bits.single_tap = true; accel.enableInterrupts(g_int_config_enabled); g_int_config_map.bits.single_tap = ADXL343_INT1; accel.mapInterrupts(g_int_config_map); } void stepFunctionadxlT4(float * x,int size_vector_1,float * y,int size_vector_2,float * z,int size_vector_3,uint16_T * TapDetection,int size_vector_4){ sensors_event_t event; accel.getEvent(&event); delay(10); *x=event.acceleration.x; *y=event.acceleration.y; *z=event.acceleration.z; *TapDetection=count; if (count>0) { /* Clearing the latch interrupt */ accel.checkInterrupts(); } }
Perform these steps to create OLED Display block.
Launch the IO Device Builder app.
Select Working Directory and Add Third-Party Source Files
Once the Source files location page loads, select the working directory and add third-party source files.
On the Source files location page:
Click Select and select the working directory. In the working directory, the generated system object, along with the corresponding cpp and h files, and the model are located.
Click Add folder and add Adafruit_SH1106-master, Adafruit_BusIO-master, and Adafruit-GFX-Library folders you downloaded. Only the files present directly within the selected folder are included, and any files present within subfolders are excluded.
Click Next to continue.
Select Source Files
On the Source files page, select the required source files and then click Next to continue.
Specify Block Parameters
On the Block parameters page:
Specify the block name and add block description.
Remove parameters for the block, if any already added.
Click Next to continue.
Select Outputs for the Block
On the Outputs page:
Remove Output ports for the block, if any already added.
Click Next to continue.
Select Inputs for the Block
On the Inputs page:
Add inputs
x
,y
,z
, andTapCount
for the block, with Data type asdouble
and Dimension as[1,1]
.Click Next to continue.
Preview Block
On the Block image page, view the preview of the block with the inputs and outputs you added. Click Next to continue.
Generate System Object Files
On the Generate page, the file generation location is displayed. Click Generate to generate the System object™ files and then click Finish in the Next Steps screen.
Perform these steps.
1. The generated .cpp
file opens automatically. Modify the generated .cpp
file as shown below.
#include "C:\user\oledDisplay\oledDisplay.h" // App generated header. Do not modify. #include "Arduino.h" #include "Wire.h" #include "Adafruit_GFX.h" #include "Adafruit_SH1106.h" #define OLED_RESET -1 Adafruit_SH1106 display(OLED_RESET); void setupFunctionoledDisplay(){ display.begin(SH1106_SWITCHCAPVCC, 0x3C); display.clearDisplay(); } void stepFunctionoledDisplay(double x,int size_vector_a,double y,int size_vector_b,double z,int size_vector_c,double TapCount,int size_vector_d){ display.clearDisplay(); display.setTextSize(1); display.setTextColor(WHITE); display.setCursor(20,2); display.print("ADXL Data: "); // Acceleration value in m/s display.setCursor(1,20); display.print("X: "); display.print(x); display.setCursor(1,30); display.print("Y: "); display.print(y); display.setCursor(1,40); display.print("Z: "); display.print(z); display.setCursor(1,50); display.print("Tap Detection: "); // Single tap detection display.print(TapCount); display.display(); }
2. Assign the output to the selected output port.
3. Add the #include "Arduino.h"
header and other required headers for the driver, as mentioned in the example with the absolute address.
4. Open the generated Simulink® model.
5. Add and connect Display blocks to the output ports.
6. In the Simulink model, navigate to Modeling > Model Settings and in the Configuration Parameters dialog box, click Hardware Implementation and then select an Arduino board and perform Monitor and Tune action.
A sample Simulink model is shown here.