Monday, April 22, 2019

Arduino Basics

Arduino is a simple, programmable micro-controller.  In this article, I am gonna write some tips which make programming with Arduino more simple.

1) Installation 

The first step will be the installation of Arduino from here:

https://www.arduino.cc/en/Main/Software

You, normally select the latest version for download, not nightly build versions.

I downloaded 1.8.9 version.

Extract the downloaded compressed file into /opt/arduino (for example). Just run the install.sh file inside the extracted Arduino folder.


2) Settings for Visual Studio Code

After installation is complete, we have to carry out some settings in visual studio code. First, install the Arduino extension and after installation is complete, restart IDE. Then we create a workspace folder.

~/workspace/arduino /src/Project1

Now, we open the arduino folder from VSCode IDE. Now, using

CTRL+SHIFT+P

A: Arduino:BoardConfig
B: Arduino:Initialize
C: Arduino:Select Serial Port

The final step is to define the build folder in arduino.json created in the root directory.

 "output": "build",

This is, I guess, automatically created. But to be safe, add this line.

3) Create a Program

The most interesting part is here.

Create a new program example.ino  with the following contents

void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}


Then we first verify the correctness of the program. And then upload the program.

Verification:  CTRL+SHIFT+P => Arduino:Verify  (Shortcut: CTRL+ALT+R)
Upload: CTRL+SHIFT+P => Arduino:Upload (Shortcut: CTRL+ALT+U)


Problems faced

While uploading I faced the following problem

An error occurred while uploading the sketch
avrdude: ser_open(): can't open device "/dev/ttyACM0": Permission denied

I did not know how secure is this, but I made the terminal readable and writable for all. And restarted IDE, then it worked! I ran the following command from the terminal.

sudo chmod a+rw /dev/ttyACM0 

Now, we can use the power of visual studio code to develop Arduino programs. 

Note: Because we can not place multiple files and run. To achieve this we create a folder structuer inside src folder of root.  For example:

ArduinoRoot
.....src
----------Project1
----------Project2
-------------------SubProject1










No comments:

Post a Comment