Thingy:53 is a compact nrf5340 based IoT development device from Nordic semiconductor with various kind of onboard sensors. But the number of GPIO available to control external components are very limited. Therefore PCA9685 based 16 channel pwm driver module is added to increase the controlling capabilities of thingy:53 device.
Here we will learn how to create a basic thingy:53 application to interface it with PCA9685 module connected at expansion port.
Before moving forward to creating a new application, we need to know the nRF5340 GPIO pins/communication interface which will be used to communicate with PCA9685 module. On thingy:53 module there is an expansion connector to interface external modules. GPIO pins available at expansion connector P1 also have I2C communication functionality (for details consult nRF5340 datasheet).
We can also use the Debug board connected at current measurement and debugging interface of thingy:53 device because it also contains the same nRF5340 GPIO which are connected at expansion connector P1.
For the easy of connection, we will use the debug and current measurement board connected at card edge connector of thingy:53 device.
From thingy:53 schematic we can find out that expansion interface is connected at P0.05 and P0.04 GPIO of nRF5340 soc.
P0.05 and P0.04 shown in above image represent GPIO port 0 pin 5 and pin 4 respectively and these pins can be used for I2C interfacing with external sensors. These two GPIO pins are also capable of analog inputs but in case of more than two analog signals or to interface external sensor module, we cannot use these GPIO pins as analog input only.
We will not be able to find the configuration of above mentioned GPIO pins and the second I2C interface in default pin control file, available in nRF connect SDK (its full path along with file name is given below).
C:\ncs\v2.1.0\zephyr\boards\arm\thingy53_nrf5340\thingy53_nrf5340_common-pinctrl.dtsi
In above mentioned file we won't see any configuration for P0.04 and P0.05. Therefore we will create an overlay file for the configuration of these GPIO pins, second I2C interface and PCA9685 pwm module.
Now knowing all the basic stuff about external pwm module interfacing, we can create a new application for our thingy53 and build its configuration file.
Optional:
After creating a simple hello world application and selecting its build configuration we will change the k-config of our application as shown below, for easy printing of floating point values because we might need to display fractional value therefore we need to enable fraction value printing on console by enabling the float for printf function as shown below;
After making the above mentioned changes in k-config, we need to create an overlay file for thingy:53 in order to enable and communicate with the PCA9685 pwm module. After creating the new overlay file add the following code to configure the GPIO pins as I2C interface.
&pinctrl {
i2c2_default: i2c2_default {
group1 {
psels = <NRF_PSEL(TWIM_SDA, 0, 4)>,
<NRF_PSEL(TWIM_SCL, 0, 5)>;
};
};
i2c2_sleep: i2c2_sleep {
group1 {
psels = <NRF_PSEL(TWIM_SDA, 0, 4)>,
<NRF_PSEL(TWIM_SCL, 0, 5)>;
low-power-enable;
};
};
};
And also add the following line in overlay file to enable the second I2C interface along with PCA9685 pwm module.
&i2c2 {
compatible = "nordic,nrf-twim";
status = "okay";
clock-frequency = <I2C_BITRATE_STANDARD>;
pinctrl-0 = <&i2c2_default>;
pinctrl-1 = <&i2c2_sleep>;
pinctrl-names = "default", "sleep";
pca9685: pca9685@40 {
compatible = "i2c-device";
reg = <0x40 >;
label = "PCA9685";
};
};
Now we can add the pca9685.c and pca9685.h files (attached below) in src directory of our current application of thingy:53. The library files of PCA9685 pwm module will not be added automatically to our make configuration. In order to achieve that we need to make some changes in the CMakeLists.txt file of our application. This configuration file can also be accessed from one the side panels.
Change the following line in
target_sources(app PRIVATE src/main.c)
as shown below after adding the pca9685.c and pca9685.h files in src directory.
target_sources(app PRIVATE src/main.c src/pca9685.c)
Now change the main.c code of hello_world application with the main.c code attached below. Then we can successfully build and flash it to thingy53 using external SEGGER J-Tag probe. When thingy53 firmware programming is finished, we can also print the debug information through RTT interface, available under CONNECTED DEVICES panel, to read the LOG messages.
PCA9685 PWM Demo Video:Note:
1)main.c, pca9685.c and pca9685. and overlay files are attached below.
2) thingy:53 pinout details link: here
3) thingy:53 debug and current measurements board pinout details link: here
Comments