Arduino Notes

From The World according to Vissie
Jump to navigation Jump to search

Install

Arduino-Cli

https://peppe8o.com/connecting-raspberry-pi-zero-w-to-arduino-only-via-terminal/

Create a directory where user executables will be stored:

sudo mkdir -p /local/bin

add this directory to user $PATH variables (so that you can execute arduino-cli without typing full path):

sudo vim /etc/bash.bashrc

add the following line to the end of file:

export PATH="/local/bin:$PATH"

logout/login to make it running. Now install arduino-cli with default script. Note the BINDIR variable telling the script to use our

sudo su
cd /local/bin/
curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | BINDIR=/local/bin sh
exit

Now create the default configuration file (suggested from arduino-cli man page to avoid a lot of typing when issuing each command):

arduino-cli config init

Connect Arduino Uno / Elegoo Uno Board Before installing our board, we need to update the local cache of available platforms and libraries:

arduino-cli core update-index

Connect Elegoo Uno/Arduino board to your Raspberry PI using the miniUSB-to-USB cable. You should be able now to list your board:

arduino-cli board list
Port         Type              Board Name  FQBN            Core
/dev/ttyACM0 Serial Port (USB) Arduino Uno arduino:avr:uno arduino:avr
/dev/ttyAMA0 Serial Port       Unknown

If you can see only Unknown port, a Raspberry PI reboot could help in refreshing hardware recognition.

In not, search to find the source for your bloard:

arduino-cli core search nano
ID              Version Name                                        
arduino:avr     1.8.3   Arduino AVR Boards                          
arduino:mbed    1.1.6   Arduino nRF528x Boards (Mbed OS)            
arduino:megaavr 1.8.6   Arduino megaAVR Boards                      
arduino:samd    1.8.8   Arduino SAMD Boards (32-bits ARM Cortex-M0+)

I installed the following too:

arduino-cli core install arduino:megaavr
arduino-cli core install arduino:samd

In the end, that did not help at all!

List all installed boards:

arduino-cli board listall

My output shows that I have an Arduino Uno based board, with FQBN (fully qualified board names) set to “arduino:avr:uno”.

Install your board:

arduino-cli core install arduino:avr

Verify that installed board is properly running:

arduino-cli core list

ID          Installed Latest Name
arduino:avr 1.8.2     1.8.2  Arduino AVR Boards

Test Your Installation

Create Your First Sketch To create a new sketch, simply type:

arduino-cli sketch new MyFirstSketch 

This will create a “MyFirstSketch” directory inside pi user home folder, containing “MyFirstSketch.ino” file inside.

Edit sketch content with common board LED control sketch (same as BLINK example):

vim MyFirstSketch/MyFirstSketch.ino

and modify like following:

void setup() {

  pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(4000);
  digitalWrite(LED_BUILTIN, LOW);
  delay(1000);
}

Before uploading our skecth, we need to compile it (the very first time, compile may take a few seconds) using your fqbn:

arduino-cli compile --fqbn arduino:avr:uno MyFirstSketch
arduino-cli compile --fqbn arduino:avr:nano MyFirstSketch
arduino-cli compile --fqbn arduino:avr:nano:cpu=atmega328old MyFirstSketch  [worked in the end!]
Sketch uses 932 bytes (3%) of program storage space. Maximum is 30720 bytes.
Global variables use 9 bytes (0%) of dynamic memory, leaving 2039 bytes for local variables. Maximum is 2048 bytes.

Upload the sketch using your phisical port and fqbn:

arduino-cli upload -p /dev/ttyACM0 --fqbn arduino:avr:uno MyFirstSketch
arduino-cli upload -p /dev/ttyUSB0 --fqbn arduino:avr:nano MyFirstSketch
arduino-cli upload -p /dev/ttyUSB0 --fqbn arduino:avr:nano:cpu=atmega328old MyFirstSketch -t -v [worked in the end!]

In the above commands you will see:

-t ..this checks if everything wen well after uploading
-v ..this means 'verbose', and will result in much more details being spewed out onto your screen about the progress of the upload.

I had Permission denied. I added myself to the correct group:

ls -l /dev/ttyUSB0 
crw-rw---- 1 root dialout 188, 0 Aug 27 14:37 /dev/ttyUSB0
sudo adduser username dialout

This way you should see your Arduino board reprogramming itself and executing the sketch (4 seconds built-in LED on, 1 second off).

Tips To Simplify Command Typing fqbn and port every time can be really boring. An help comes from linux alias command. The following line will associate “acompile” with arduino-cli compile and its full parameters. The second line will make the same work but with the update command:

alias acompile="arduino-cli compile --fqbn arduino:avr:uno"
alias aupload="arduino-cli upload -p /dev/ttyACM0 --fqbn arduino:avr:uno"

Now you can complile and upload sketches simply by typing:

acompile MyFirstSketch/
aupload MyFirstSketch/

or with combined command:

acompile MyFirstSketch/ && aupload MyFirstSketch/

Libraries

First update your library index with:

arduino-cli lib update-index

Then search for a library, like neopixel:

arduino-cli lib search neopixel

Install the library by its name, for example "Adafruit NeoPixel":

arduino-cli lib install "Adafruit NeoPixel"

Confirm it is installed with:

arduino-cli lib list

Libraries are installed in your Documents\Arduino\libraries\ directory by default. Core libraries are stored in a different location. In Windows they are in an Arduino directory in the AppData\Local directory of your user's home.

/home/user/Arduino/libraries/Firmata/examples/StandardFirmata/StandardFirmata.ino

To uninstall a library, use the name of the folder in the libraries directory, which might be different from the install name. For example, to uninstall "Adafruit Zero DMA Library" you must use

arduino-cli lib uninstall "Adafruit_NeoPixel"

pyFirmata

https://realpython.com/arduino-python/

A nice way to communicate with a Arduino that is permanently connected. I'll give it a go for now and see.

LED and Rotary Encoder

Rotary Encoder:

https://www.circuitsathome.com/mcu/reading-rotary-encoder-on-arduino 

Button:

http://playground.arduino.cc/Code/HoldButton