This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| en:iot-open:remotelab:ume:smartme:b3 [2019/09/27 15:02] – created salvatdi | en:iot-open:remotelab:ume:smartme:b3 [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | B3: PWM mode (~) of digital pins, programming threshold values | + | ==== IB3: PWM mode (~) of digital pins, programming threshold values |
| + | |||
| - | Target group | + | === 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. | 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. | + | === Prerequisites |
| - | RGB LED. All the nodes of SmartME Network Laboratory are equipped with an RGB LED of which, however, | + | |
| + | == Liquid Crystal | ||
| + | For this library, you may refer to the [[en: | ||
| + | |||
| + | == RGB LED == | ||
| + | The SmartME Network Laboratory | ||
| + | To connect other devices, however, only the green light component | ||
| To turn on the green LED, you have to follow three steps: | To turn on the green LED, you have to follow three steps: | ||
| - | first, you have to define a variable | + | - first, you have to define a variable |
| - | second, you need to configure this variable in output mode (OUTPUT). You do this with a call to the pinMode(pin, | + | |
| - | finally, you need to send to this variable a HIGH signal by using the digitalWrite(pin, | + | |
| + | To turn off the LED, you need to send a LOW signal value to this pin. | ||
| + | You can make the LED flashing | ||
| The digital pins either give you 5V (when turned HIGH) or 0V (when turned LOW) and the output is a square wave signal. | 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. | 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. | + | 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 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: | + | == The circuit: |
| RGB GREEN pin = Arduino pin 3 (~) | 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, | + | === Scenario |
| - | Result | + | |
| + | Initialize the LCD screen with the label “rand value”, then calculates a random value. If randomValue is lower than lowThreshold, | ||
| + | |||
| + | === 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. | 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 | + | |
| + | === Start === | ||
| There are no special steps to be performed. | There are no special steps to be performed. | ||
| - | Steps | + | === Steps === |
| - | Step 1 | + | |
| - | Include LCD driver library: | + | == Step 1 == |
| + | Include | ||
| + | <code c> | ||
| #include < | #include < | ||
| - | Step 2 | + | </ |
| + | |||
| + | == Step 2 == | ||
| Instantiate the software controller component for the LCD display. Initialize the LED pin. Declare the threshold values. | Instantiate the software controller component for the LCD display. Initialize the LED pin. Declare the threshold values. | ||
| + | |||
| + | <code c> | ||
| // initialize the library with the numbers of the interface pins | // initialize the library with the numbers of the interface pins | ||
| LiquidCrystal lcd(7, 6, 5, 4, 13, 2); | LiquidCrystal lcd(7, 6, 5, 4, 13, 2); | ||
| - | |||
| // initializing LED Pin | // initializing LED Pin | ||
| int greenPin = 3; | int greenPin = 3; | ||
| - | |||
| // declaring the threshold values | // declaring the threshold values | ||
| int lowThreshold = 127; | int lowThreshold = 127; | ||
| int highTreshold = 128; | int highTreshold = 128; | ||
| + | </ | ||
| - | | + | == Step 3 == |
| - | + | Initialize | |
| - | + | ||
| - | Initialize display and declare the LED pin as OUTPUT - we suggest to do it in setup() function: | + | |
| + | <code c> | ||
| void setup(){ | void setup(){ | ||
| - | + | pinMode(greenPin, | |
| - | | + | |
| - | | + | lcd.print("rand value"); // print the random value label to the LCD |
| - | + | ||
| - | | + | |
| - | lcd.begin(16, 2); | + | |
| - | | + | |
| - | lcd.print(" | + | |
| } | } | ||
| + | </ | ||
| - | Step 4 | + | == Step 4 == |
| Implement loop() to do in the order: | Implement loop() to do in the order: | ||
| calculate a random value between 0 and 255; | calculate a random value between 0 and 255; | ||
| Line 67: | Line 81: | ||
| + | <code c> | ||
| void loop() { | void loop() { | ||
| - | | ||
| // calculate a randomValue in the range 0-255 | // calculate a randomValue in the range 0-255 | ||
| int randomValue = random(255); | int randomValue = random(255); | ||
| - | | ||
| - | // if the randomValue is lower than | ||
| if (randomValue < lowThreshold){ | if (randomValue < lowThreshold){ | ||
| digitalWrite(greenPin, | digitalWrite(greenPin, | ||
| } | } | ||
| - | | ||
| if (randomValue > highTreshold){ | if (randomValue > highTreshold){ | ||
| digitalWrite(greenPin, | digitalWrite(greenPin, | ||
| } | } | ||
| - | + | | |
| - | delay(1000); | + | |
| - | + | ||
| - | // print the random value of light to the LCD | + | |
| | | ||
| | | ||
| - | |||
| | | ||
| | | ||
| - | |||
| } | } | ||
| + | </ | ||
| - | Result validation | + | |
| - | Observe the state of the LED for 1 second, this status | + | === Result validation |
| + | The state of the LED will depend on the random number calculated and on the threshold values that have been set. | ||
| + | The random value will be shown by the display monitor. | ||
| + | It also will affect | ||
| - | Platformio.ini | + | === Platformio.ini |
| - | + | < | |
| [env:uno] | [env:uno] | ||
| platform = atmelavr | platform = atmelavr | ||
| board = uno | board = uno | ||
| framework = arduino | framework = arduino | ||
| - | |||
| lib_ldf_mode=deep+ | lib_ldf_mode=deep+ | ||
| lib_compat_mode=strict | lib_compat_mode=strict | ||
| - | + | lib_deps = | |
| + | DHT sensor library@1.3.0 | ||
| + | | ||
| lib_deps_external = | lib_deps_external = | ||
| | | ||
| + | </ | ||
| + | === IB3.cpp === | ||
| - | B3.cpp | ||
| + | <code c> | ||
| #include < | #include < | ||
| #include " | #include " | ||
| - | |||
| - | #include < | ||
| - | |||
| // initialize the library with the numbers of the interface pins | // initialize the library with the numbers of the interface pins | ||
| LiquidCrystal lcd(7, 6, 5, 4, 13, 2); | LiquidCrystal lcd(7, 6, 5, 4, 13, 2); | ||
| Line 124: | Line 133: | ||
| int lowThreshold = 500; | int lowThreshold = 500; | ||
| int highTreshold = 600; | int highTreshold = 600; | ||
| - | + | ||
| void setup(){ | void setup(){ | ||
| // declaring LED pin as output | // declaring LED pin as output | ||
| Line 133: | Line 142: | ||
| lcd.print(" | lcd.print(" | ||
| } | } | ||
| - | + | ||
| void loop() { | void loop() { | ||
| - | |||
| int randomValue = random(255); | int randomValue = random(255); | ||
| - | |||
| if (randomValue < lowThreshold){ | if (randomValue < lowThreshold){ | ||
| digitalWrite(greenPin, | digitalWrite(greenPin, | ||
| } | } | ||
| - | |||
| if (randomValue > highTreshold){ | if (randomValue > highTreshold){ | ||
| digitalWrite(greenPin, | digitalWrite(greenPin, | ||
| } | } | ||
| - | | ||
| | | ||
| - | |||
| // print the random value of light to the LCD | // print the random value of light to the LCD | ||
| | | ||
| | | ||
| - | |||
| | | ||
| | | ||
| - | |||
| } | } | ||
| - | + | </ | |