B3: Handling LED brightness using PWM via I2C controller

This is the simple example of setting brightness of LED using PWM controller.

Target group

This hands-on lab guide is intended for the Beginners.

Prerequisites

The user needs to know: Basic knowledge of ESP8266 NodeMCU v.2. ESP8266 Arduino programming, Basic knowledge of I2C Interface. Basic knowledge of I2C programming using Arduino I2C library.

Scenario

All important information including the address of devices is enclosed in the description of the laboratory. In that scenario, the user should write a program in the Arduino platform which turns on and off LEDs for the specified time 15 seconds. The LEDs brightness can be controlled by setting variable “level” from 0 to 4095.

Result

As a result user can control in cameras program execution.

Start

In the beginning, both cameras must show that all LEDs are off.

Steps

Step 1

First load an example project, include appropriate libraries and declare variable ledDriver:

#include <Wire.h>
#include <PCA9685.h>

Step 2

Instantiate PWM controler component:

#define PCA9685_ADDRESS     0x40
PCA9685 ledDriver; 

or

#define PCA9685_ADDRESS     0x40
PCA9685 ledDriver(PCA9685_ADDRESS); 

Step 3

Initialize properly hardware, we suggest to do it in setup() function:

First, initialize I2C controller and join i2c bus to correct pins of NodeMCU:

  ...
  Wire.begin(D1, D2); /* join i2c bus with SDA=D5 and SCL=D4 of NodeMCU */
  ...

and then enable PWM controller ( pin /EN of PCA9685 is connected to pin D0 of NodeMCU):

  ...
  pinMode( D0, OUTPUT );  // define pin D0 as output 
  digitalWrite( D0, LOW); // enable PWM
  ...

Step 4

Turn the desired led to any PWM level from 0 to 4095:

The diodes are numbered from 0 to 3
PWM level 0 means that LED is off and level value 4095 means that diode is full-on.
    ...
    ledDriver.setLEDDimmed( number , level);
    ...
Function setLEDDimmed cannot be used in a loop to give you a pleasant
"turning-up" of the LED. This is because each time you set a level for a LED
it will calculate random timing intervals for the PWM function in the chip
This is done in order to distribute current consumptions of the full-time period.

Step 5

Write a simple programme which:

  • Turn LED0 placed on the bright side to warm-white fully on for 15 seconds
  • Turn LED0 off
  • Turn LED1 placed on the bright side to cold-white fully on for 15 seconds
  • Turn lED1 off
  • Turn LED2 placed on the dark side to warm-white fully on for 15 seconds
  • Turn LED2 off
  • Turn LED3 placed on the dark side to cold-white fully on for 15 seconds
  • Turn lED3 off

Step 6

Repeat step 5 ten times.

Result validation

The only way for validation is to check display by the camera. Both cameras must show that LEDs are changing brightness in given periods of time.

en/iot-open/remotelab/sut/color/b4.txt · Last modified: 2020/07/20 09:00 by 127.0.0.1
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0