This is an old revision of the document!
B3: PWM mode (~) of digital pins, programming threshold values
Target group This hands-on lab guide is intended for the Beginners but other target groups may benefit from it, treating it as a tool for advanced projects. Prerequisites Liquid Crystal library, you may refer to the B1 exercise. RGB LED. All the nodes of SmartME Network Laboratory are equipped with an RGB LED of which, however, at the moment it is only possible to use the green light component. This single colour component can be considered and effectively managed as a separate LED. To turn on the green LED, you have to follow three steps: first, you have to define a variable int that will hold the number of the pin that the LED is connected to (int greenPin = 3;); second, you need to configure this variable in output mode (OUTPUT). You do this with a call to the pinMode(pin, mode) function, inside the setup() function. finally, you need to send to this variable a HIGH signal by using the digitalWrite(pin, value) function, inside the loop() function. To turn off the LED, you need to send a LOW signal value to this pin. You can make the LED flash by changing the length of the HIGH and LOW states. The digital pins either give you 5V (when turned HIGH) or 0V (when turned LOW) and the output is a square wave signal. PWM stands for Pulse Width Modulation and it is a technique used in controlling the brightness of the LED. The PWM pins are labelled with ~ sign. The function analogWrite() can be used to generate a PWM signal in those digital pins that are labelled with ~ sign. The frequency of this generated signal for most pins will be about 490Hz and you can give the value from 0-255 using this function. analogWrite(0) means a signal of 0% duty cycle. analogWrite(127) means a signal of 50% duty cycle. analogWrite(255) means a signal of 100% duty cycle. The function random() generates pseudo-random numbers. The syntax is random(max) or random(min, max), where min represents the lower bound of the random value, inclusive (optional); and max represents the upper bound of the random value, exclusive. The function returns a long random number between min and max-1.
The circuit: RGB GREEN pin = Arduino pin 3 (~) Scenario Initialize the LCD screen with the label “rand value”, then calculates a random value. If randomValue is lower than lowThreshold, turn on the LED. If randomValue is upper than highThreshold, turn off the LED. After 1 second, print the random value to the LCD. Then, turn on the LED for 3 seconds, assigning it a value of brightness which corresponds to the random value previously calculated. Note, you can set the two values of the threshold as you prefer. You can modify the brightness of the LED because, although it is connected to a digital pin, the Arduino pin number 3 is labelled with ~ sign (PWM). Result You should see the LED on or off for 1 second, a second if the value is greater or less than the predefined threshold values. Next, you should see the randomly calculated value between 0 and 255 on the LCD, at that point, the LED should assign a brightness associated with that value, and keep it for 3 seconds. Start There are no special steps to be performed.
Steps Step 1 Include LCD driver library: #include <LiquidCrystal.h> Step 2 Instantiate the software controller component for the LCD display. Initialize the LED pin. Declare the threshold values. initialize the library with the numbers of the interface pins LiquidCrystal lcd(7, 6, 5, 4, 13, 2); initializing LED Pin int greenPin = 3;
declaring the threshold values int lowThreshold = 127; int highTreshold = 128; Step 3 Initialize display and declare the LED pin as OUTPUT - we suggest to do it in setup() function: void setup(){ declaring LED pin as output
pinMode(greenPin, OUTPUT);
// set up the LCD's number of columns and rows
lcd.begin(16, 2);
// print the random value label to the LCD
lcd.print("rand value");
}
Step 4 Implement loop() to do in the order: calculate a random value between 0 and 255; compare the random value with the lower threshold and with the upper threshold in order to decide whether to switch the LED on or off for 1 second; display the random value on the screen, and use it to assign a specific brightness value to the LED, which must be held for 3 seconds.
void loop() {
// calculate a randomValue in the range 0-255
int randomValue = random(255);
// if the randomValue is lower than
if (randomValue < lowThreshold){
digitalWrite(greenPin, HIGH);
}
if (randomValue > highTreshold){
digitalWrite(greenPin, LOW);
}
delay(1000);
print the random value of light to the LCD lcd.setCursor(14, 0); lcd.print(randomValue); analogWrite(greenPin, randomValue); delay(3000); } Result validation Observe the state of the LED for 1 second, this status will depend on the random number calculated and the threshold values that have been set. Observe the random value calculated on the display monitor. Observe how the random value affects the LED brightness for 3 seconds. Platformio.ini [env:uno] platform = atmelavr board = uno framework = arduino lib_ldf_mode=deep+ lib_compat_mode=strict lib_deps_external = https://github.com/arduino-libraries/LiquidCrystal.git#1.0.7 B3.cpp #include <LiquidCrystal.h> #include “DHT.h” #include <LiquidCrystal.h> initialize the library with the numbers of the interface pins LiquidCrystal lcd(7, 6, 5, 4, 13, 2); initializing LED Pin int greenPin = 3; declaring of threshold values int lowThreshold = 500; int highTreshold = 600;
void setup(){
// declaring LED pin as output
pinMode(greenPin, OUTPUT);
// set up the LCD's number of columns and rows
lcd.begin(16, 2);
// print the random value label to the LCD
lcd.print("rand value");
}
void loop() {
int randomValue = random(255);
if (randomValue < lowThreshold){
digitalWrite(greenPin, HIGH); }
if (randomValue > highTreshold){
digitalWrite(greenPin, LOW); } delay(1000);
print the random value of light to the LCD lcd.setCursor(14, 0); lcd.print(randomValue); analogWrite(greenPin, randomValue); delay(3000); }