We previously discussed that Arduino has no Programming Language of it’s own. Arduino Basically For the Advanced Hobbyists. It Does Not Take Much Time to Learn Basic C, C++. But Again, How to Learn Arduino Programming So That Others’ Projects Can Be Improved OR Own Idea Can Be Realized? Arduino is an open-source prototyping platform for the makers, artists, designers, hobbyists, or anyone interested in creative things. Depending on the person’s need, amount of learning should be directed. We are explaining basics and providing some resources.
How to Learn Arduino Programming?
The language Arduino uses is like C++, but it is very different from common C++ as Arduino uses abstraction to make it simple to use. A background of Java, C and C++ will make advanced uses easy. Arduino IDE uses GCC compiler and avoid makefile, obviously it supports C++. The directories named like “core”, the libraries are C++.
Knowing about C++ programming includes learning basic loops, declarations,functions, classes and so on. C++ takes about 2 weeks to get used, know about setup, loop, basic commands specific or non-specific for Arduino like pinout, pinin, analogout, digitalread, digitawrite, analogread, analogwrite and so on.
---
Every snippet or code of Arduino is called sketch. Each sketch has two void
functions – setup()
and loop()
. Commonly void
type function itself returns no value.
setup()
is a method. setup()
is ran once after the Arduino is powered up or reset made. The loop()
method continuously run. So we learned :
1 2 | void setup() { } void loop() { } |
The above will not return any value if uploaded on Arduino board. If or hardware is a LED, then we need to define the ledPin
which will call Arduino’s function named pinMode()
. Here is reference :
1 | https://www.arduino.cc/reference/en/language/functions/digital-io/pinmode/ |
pinMode(pin, mode)
has 3 modes : INPUT
, OUTPUT
, INPUT_PULLUP
. If we used component like a push button switch instead of LED, we would use INPUT
. As LED is an output at pin 13, we will use OUTPUT
or INPUT_PULLUP
, if we add one push button at pin 4 then :
1 2 3 4 5 6 7 8 9 | int ledPin = 13; void setup() { pinMode(13, OUTPUT); pinMode (4, INPUT_PULLUP); } void loop() { } |
OR
1 2 3 4 5 6 7 8 9 10 | int ledPin = 13; int buttonPin = 4; void setup() { pinMode(13, OUTPUT); pinMode(4, INPUT); } void loop() { } |
We ended each line with a semicolon. Now what we want to do? To blink, first think of this :
Light on LED
For n seconds
Light off LED
For n seconds
That means :
1 2 3 4 | digitalWrite(13, HIGH); // sets the digital pin 13 on delay(1000); // waits for a second digitalWrite(13, LOW); // sets the digital pin 13 off delay(1000); // waits for a second |
So basic code to link becomes :
1 2 3 4 5 6 7 8 9 10 11 12 | int ledPin = 13; void setup() { pinMode(13, OUTPUT); } void loop() { digitalWrite(13, HIGH); delay(1000); digitalWrite(13, LOW); delay(1000); } |
If we use a push button turn on LED and let it remain ON still pressed, then :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | int pinButton = 4; int ledPin = 2; void setup() { pinMode(pinButton, INPUT); pinMode(ledPin, OUTPUT); } void loop() { int stateButton = digitalRead(pinButton); if(stateButton == 1) { digitalWrite(LED, HIGH); } } |
if(stateButton == 1
means button is pressed.
OR we can write :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | const int switchPin = 4; const int ledPin = 8; int switchState = 0; // reading switch status void setup() { pinMode(ledPin, OUTPUT); pinMode(switchPin, INPUT_PULLUP); } void loop() { switchState = digitalRead(switchPin); if (switchState == LOW) { digitalWrite(ledPin, HIGH); delay(3000); digitalWrite(ledPin, LOW); } } |
Here is diagram of the above :
if (switchState == LOW)
means if the switch is pressed. If you want that the LED remains ON after button is pressed, that you have to learn. Here is reference and a notebook for you for that homework :
1 2 | https://www.arduino.cc/reference/en/ https://playground.arduino.cc/uploads/Main/arduino_notebook_v1-1.pdf |
Arduino official site has good number of user created documentation, examples.