Using a Python Script to Interface the Digital Discovery with an I2C Device
In this tutorial we will use a python script to read and graphically present data from a digital accelerometer with an I2C interface (LIS35DE).
- A computer with a USB port to run the software
- Python Software with Python 2.7 interpreter and matplotlib
- An accelerometer with an I2C interface
- 2×6 MTE Cables may be used for the connections
- Python Source Code downloaded to the computer
Step 1: Hardware Setup
1. Connect GND and Vdd of the sensor to the Digital Discovery's GND and VIO pins, respectively.
2. The sensor is dual interface, SPI or I2C. It should remain in I2C mode. The CS pin of the device need to be connected to Vdd.
3. The chip has a selectable I2C address; to select 0x38 for the address, the SDO pin needs to be tied to GND.
4. SDA and SCL lines should be connected to DIO33 and DIO32, respectively.
5. These lines should have suitable pull-up resistors connected – resistors are already mounted on the selected module .
Step 2: Script
The downloaded Python script will:
1. Load WaveForms SDK
2. Connect to the Digital Discovery
3. Enable the 3.3V power supply for the sensor
4. Configure the I2C interface
To configure the demo, the user needs to set the operating frequency of the bus and select which pins of the Digital Discovery should be used.
Note: The SDK functions related to digital output, or input/output (such as for the I2C interface) start counting channels from DIO24. DIO24 will be referenced as 0, DIO 25 as 1, etc.
# set I2C frequency and pins dwf.FDwfDigitalI2cRateSet(hdwf, c_double(1e5)) # 100kHz dwf.FDwfDigitalI2cSclSet(hdwf, c_int(32-24)) # SCL = DIO-32 dwf.FDwfDigitalI2cSdaSet(hdwf, c_int(33-24)) # SDA = DIO-33
5. Initialize the sensor and display
6. Read sensor data and update graphs as long as the plot window is open
Each read operation from the sensor is a write-read operation on the I2C bus. A device address is set for a complete operation and WaveForms SDK automatically maintains the R/W bit. Two arrays are used to write and read data, and user specifies how many bytes should be written, and , after repeated start, read. In our case only one byte is written. The register address is combined with a bit mask indicating that the sensor should increment the register address after each byte is read from the bus. Then, 5 bytes are read from the subsequent registers beginning at the OUTX register. The read data contains 3 bytes with X, Y and Z acceleration data represented in two’s complement and two dummy bytes.
dwf.FDwfDigitalI2cWriteRead(hdwf, c_int(address << 1), (c_ubyte*1)(LIS35DEreg.REG_OUTX | LIS35DEreg.READ_INC), c_int(1), rgRX, c_int(5), byref(iNak))
7. After closing the plot window, disable the power supply and disconnect from the device
Step 3: Running the Demo
Before running the script make sure that all the connections are correct, and place the sensor on a flat surface for a “virtual spirit level” calibration. When ready, run the script. The following window should appear.
The upper part of the window presents live XYZ acceleration from the sensor as a function of time, while the lower part presents the actual X and Y acceleration as a blue asterisk (*) – when the sensor is in the same orientation as at the script start-up the asterisk is in the center – it behaves as a spirit level.
After closing the window the script will shut down the sensor and disconnect from the Digital Discovery.
Step 4: Wrapping Up
Using a Digital Discovery and WaveForms SDK it is possible to communicate with a representative digital sensor. With a little effort, communication and configuration may be tested, as well as a simple graphical interface be presented. Some automatic testing routines may be implemented for design control.