Arduino101

From XPUB & Lens-Based wiki
Revision as of 21:11, 30 September 2019 by Ddebel (talk | contribs) (→‎Output)


Arduino is umbrella term for a collection of programmable microcontrollers. These microcontrollers allows you to easily interface the physical world of knobs and 'sensors' with code. The term 'arduino' covers both a physical device and an integrated development environment (IDE) that allows you to write, review, debug and upload code to your microcontroller. The Arduino IDE supports a wide variety of microcontrollers from different manufacturers with equally varying specifications (ATMEL, ESP32, ESP8266, etc). The original Arduino project takes a bare 8 bit microcontroller and adds an USB port (+ usb-to-serial converter by FTDI), voltage regulators, input/output header pins for plug and play experience. It's schematics are open source and allowed the proliferation of many varieties and clones. It is also very easy (and cheap) to make your own 'Arduino' clone on a breadboard with a handful of components.


Software

NOTE: Arduino clones need a special driver, see below.

Graphical

All Platforms (Windows/OSX/Linux) >> Download the IDE here: https://www.arduino.cc/en/Main/Software

Installation OSX Unpack, mount, drag to applications folder

Installation Win Run installer, follow installation procedure.

Installation Linux https://www.instructables.com/id/Install-Arduino-IDE-182-on-Linux/

Command Line

First, install the Graphical IDE, then: https://github.com/sudar/Arduino-Makefile

See also: https://git.xpub.nl/XPUB/special-issue-x/src/branch/master/templates/arduino-make and https://git.xpub.nl/XPUB/special-issue-x/src/branch/master/templates/bare-make

Driver

WIN+OSX:http://sparks.gogo.co.nz/ch340.html OSX Mojave has the driver built in. Recent Linux flavours have the driver built in.

Web

https://www.circuito.io/ https://create.arduino.cc/editor


Hardware

To program (or flash) your arduino, you need to connect it to your computer (using USB). Connect your Arduino, open the Arduino IDE and see if your board appears:

Arduino-uno-origiginal-serial.png
Screenshot showing original Arduino UNO connected to serial port on OSX.

Important settings
Board: Select the correct Arduino AVR Board from the list (UNO or Nano in our case). Note in the case of the Nano, there are 2 different chips, 328p and 328p (Old bootloader). If you get errors while uploading your sketch, play with this option.

Port: Select the right serial port to upload your sketch to:
linux: /dev/ttyACM# or /dev/ttyUSB (# = number)
osx: /dev/cu.wchusbserial# or dev/cu.usbmodem# (# = number)
win: COM# (# = number)


Arduino-nano-clone-serial.png
Screenshot showing clone Arduino Nano connected to serial port on OSX.

Arduino-bootloader.png
Screenshot showing clone Arduino Nano 'bootloader' options on OSX.

Sketches

Anatomy of a sketch

Arduino programs/firmware are called 'sketches'. The language used it a variant of C/C++. When you open a new sketch (File > New), you are presented with the following code:

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

Void

Void defines a function that does not return any values after being executed.

Setup()

Setup is a function that is only run once, when the arduino is powered on. Everything inside is executed only once.

Loop()

Loop, is an ever repeating function while the Arduino is powered on. Everything inside is repeated over and over. Loop, is an ever re....

Comments

To add notes/instructions or to temporary disable a piece of code you can use comments. Indicated by the double forward slashes: //

Semicolon

Every line ends with a semicolon: ;

Variables

Entities used for (dynamically) storing values, strings etc. int, float, char.

Delay()

Add milliseconds delay to your loop, Arduino runs at 4, 8 or 16MHz...thats too fast to see!

Debugging

To see if your code is running, or to see what values are being read by the inputs or send to the outputs you can activate the serial console. Add the following code in in the setup() function:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("hello!");
}

void loop() {
  // put your main code here, to run repeatedly:

}

Arduino-serial-monitor0.png activate monitor after uploading your code


Arduino-serial-monitor1.png Output of the above code

Basic Loops: For

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("hello!");
}

void loop() {
  // put your main code here, to run repeatedly:
  for (int i = 0; i <= 255; i++) {
      Serial.println(i);
    delay(100); // pause loop for 100ms
  }

}

Basic Control Structures: If

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  Serial.println("hello!");
}

void loop() {
  // put your main code here, to run repeatedly:
  for (int i = 0; i <= 255; i++) {
    if (i == 100) {
      // if count is 100, do something!
    }
    delay(20); // pause loop for 100ms
  }

}


Exercise: make the serial monitor say something when count is below 100, when count is 100 and when count is higher than 100.
hints: https://www.arduino.cc/reference/en/language/structure/control-structure/if/

Arduino + Processing

Using Serial for Visualising, also add cli way (for more flexible/modular approach(promoting approaches not apps))! https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/all

Prototyping

Breadboard

Breadboard101.png
Solderless breadboard, (hand drawn) red lines indicate interconnected contacts

Input

Rather than talking about 'sensors' (marketing term), lets look at practices involved in creating and manipulating data from various components attached to your arduino board.


Pushbutton

Voltage divider

In practice most sensors are variable resistors: limiting current according to light, humidity, sound, mechanical stress (button, bend sensors, piezo's)

Potentiometer

Variable resistor (mechanical)

LDR

Variable resistor (light). Application: Fake Theremin, reacting to (phone) screen

Smoothing in Software

Software smoothing and filtering: https://www.megunolink.com/articles/coding/3-methods-filter-noisy-arduino-measurements/

Output

Blinking LED

Blink a LED (every Arduino board has a built in LED):

// the setup function runs once when you press reset or power the board
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
}

Protect ya Led: External LED

A LED (Light Emitting Diode) is a diode and thus has polarity. In one direction it blocks current and the other direction allows infinite current. This infinite current thirst can cause the LED to burn through. We must limit the current using a resistor (even a 3v battery can destroy a LED when connected directly):
Arduino-uno-circuito-led.png See online sketch here

Online Led protection resistance calculator and rationale

Sound

(Tone?) Arduino UNO > http://highlowtech.org/?p=1963

DAC

Arduino can convert analog signals (voltages) into 8 bit digital numbers (0-1023 using an 'ADC' or Analog to Digital converter). The arduino cannot output analog voltages only digital suqare waves in the shape of a PWM (Pulse Width Modulation) signal. To retireve analog voltages you need external hardware.

Simple Hardware

R2R ladder filter, RC Filter (filter harmonics from the pwm), https://provideyourown.com/2011/analogwrite-convert-pwm-to-voltage/

Intermediate Hardware

https://create.arduino.cc/projecthub/Arduino_Scuola/build-a-simple-dac-for-your-arduino-4c00bd.

Hard Hardware

https://www.electroschematics.com/arduino-dac-guide/

Considerations

Clones vs Real Deal

Clones use inferior usb to serial chips, voltage regulators and non removable atmel microcontrollers. With buying an original Arduino you support the development and community (needs proof ;p).