The Internet of things (IoT) is the network of devices, vehicles, and home appliances that contain electronics, software, actuators, and connectivity which allows these things to connect, interact and exchange data
This may seem complicated at first but it’s becoming easier and easier for people with a basic programming understand or electrical understanding, to break into. It comes in many forms, such as Arduino and Raspberry Pi. In this post I will be talking mainly about Arduino and have put together some example code which can be found here. This is not coding tutorial, but try’s to explain some Arduino concepts and how to use them.
Arduino is used for prototyping. It is not a computer unlike Raspberry Pi. It uses c++ but having a prior knowledge of it isn’t essential to get things working for basic applications. It is also open source, which means that the IDE is easily downloadable here . It also means that non-official boards are capable of running files with the .ino extension. There is definitely a difference between Arduino official boards but just know that there are cheaper alternatives.
Above is the barebones of Arduino. When creating you own sketches you can declare global variables above the setup function. Pins and variables can be initialized in the setup function. This function is run once upon start up and typically is used to tell the board what pins you will be working with and whether they will be output or input. It is also where the serial connection will start.
The loop function will loop continuously whilst the board is on. This is the body where all working code should be executed. If you plan on using functions then you can declare these outside of the loop and call them within it as they will have global scope providing that they are not written in the setup function.
At the bottom you have a console which will kindly let you know when your code doesn’t work.
References for Arduino can be found here.
To follow the code you will need;
- an Arduino board – I am using a Nano, but any board that is capable of PWD will do.
- a breadboard.
- RBG LED – An LED will do, just make sure it’s connected to the pins in the code.
- Resistors – Resistors up to 1k ohm.
In my setup I am using an Anode LED, which means the polarity is reversed compared to a standard Cathode LED. To check which polarity your LED uses connect the longest pin on the LED to the GND pin on the board and the 5v pin to any of the other pins on the LED. If it lights up you are using a Cathode, otherwise Anode. If you find you are using a Cathode switch the HIGH values in the code to LOW and vice versa.
I have my LED connected to pins 11, 10 and 9. If you connect you LED differently, update the code to reflect this. Also the fade file depends on the pins being capable of PWD so bare this in mind. If you are unsure consult the documentation of your board.
To follow along copy the code in simpleLEDBlink/simpleLEDBlink.ino file into your Arduino IDE.
As you can see above, I set the pins to variables that represent the LED color. These will never change so I have made them constant. In the setup each is set to output using the arduino syntax of pinMode. This allows for the board to know which pins it will be working with and that they will output rather than input(I won’t receive any data from them). The digitalWrite syntax is used to set the LED to off(if using cathode switch these to LOW).
These are the above functions that we will be working with. These are declared outside of the loop and can be called within it as they are global functions. Comment out the blink function calls in the loop and see the LED blink.
The blink function receives the constant variables we declared at the top of the file and a time interval. We use digitalWrite() to turn the LED on, use delay() to wait an amount of time in ms and then turn off the LED with digitalWrite(). digitalWrite() is Arduino syntax and takes in a pin and a value of either HIGH or LOW. Digital signals work by either being on or off, 1 or 0. HIGH will send power one way and LOW will send it the other. Knowing the polarity of your LED first will help you to understand the behaviour.
Running the three commented out blink() calls will blink red, green and blue continuously. Feel free to change the code to maybe blink the whole LED.
Blinking more or less
Look at the moreOff() and moreOn() functions. Comment out the blink() functions within the loop and comment in the moreOn(). After comment out the moreOn() and comment in the moreOff(). See the difference?
Micro-processors and computers in general work at a much faster speed than the human brain. The moreOff() function appears to be dimmer than the moreOn() function as the LED spends more time off than it does on. This is great for us as programmers and tinkerers. So what exactly is going on?
This is a basic demonstration of PWM(Pulse Width Modulation) which is a technique used for getting analog results whilst using a digital signal. NOTE: Analog signals such as FM radio, operate through varying frequencies. It’s an old technology but still has a lot of applications. Computers use digital signals, so PWM is used to gain control of electronics that we might want to vary signals. Having an LED just blink is okay, but what if we want to fade. PWM!
You can even see that the LED is switch on and off whilst executing both functions. Changing the difference between the delays will make it appear more dim or more bright depending on whether the LED spends more time off or on.
Above is a chart detailing what is actually going on. The code works okay but its definitely still blinking and would require a lot of maths to fade up or down. That’s okay Arduino has us covered by having digital pins that are capable of PWM and analog functions!
Getting that smooth FADE
Load up the code in simpleLEDFade/simpleLEDFade.ino file into Arduino IDE.
Not much has changed from the previous code. I’ve declared some extra values which will help to display output in the console. Also i’ve used the Serial.begin(). This function allows for communication between the board and the computer. We will be operating on 9600.
The above functions take in an LED as an argument. To fade up we loop and decrement from 255 down to 0. We use the analogWrite() function to set the LED to the i value. The analogWrite() function works with numbers between 0 and 255, in my case 255 means the LED will be off and 0 means it will have maximum brightness. Using the LED passed in as an argument allows me to keep track of what value the LED currently has and is used for the print out in the console using the Serial.println() function. A delay is used so that we can see the change, alter this for a faster or slower fade.
– This can be found in the upper right corner of the IDE and is how we get the console up. Ensure that the 9600 value is selected in the dropdown box at the bottom of the page.
Putting it all together
simpleColorFade() fades each color of the LED up and down sequential. colorFade() fades and blends colors together. In the console you see the appropriate RGB values for the colors.
Feel free to play around with the code and add to it. The sketches are basic and are only meant to demonstrate the basic application of Arduino. If you want to learn more tutorials can be found here and instructables has many project ideas with tutorials to get started. Also check out the example code in the file tab of the Arduino IDE.