Arduino Programming and Electronics Fundamentals
Arduino Programming Fundamentals
setup()
Function
The setup()
function is called when your program starts. Use it to:
- Initialize variables
- Set pin modes
- Start using libraries
- Configure other initial settings
The setup()
function will only run once, after each power-up or reset of the Arduino board.
loop()
Function
After creating a setup()
function, which initializes and sets the initial values, the loop()
function does precisely what its name suggests: it loops consecutively, allowing your program to change and respond. Use it to actively control the Arduino board.
Variables
Variables let you store a value. You must declare variables and tell the computer what kind of value to expect.
- int: Number between -32,768 to 32,767
- byte: 0 – 255
- char: Character type
Data Types
Integer
An integer is a whole number (e.g., 1, 2, 3, 25). Integers are your primary data type for number storage and store a 2-byte value. This yields a range of -32,768 to 32,767.
char
A data type that takes up 1 byte of memory to store a character value. For example, both of these are equivalent and refer to the letter A:
byte
A byte stores an 8-bit unsigned number, from 0 to 255.
boolean
A boolean holds one of two values: true or false. Each boolean variable occupies one byte of memory.
Long
Long variables are extended-size variables for number storage.
float
A number that has a decimal point. Floating-point numbers are often used to approximate analog and continuous values because they have greater resolution than integers.
Array
An array is a collection of variables that are accessed with an index number. Arrays are zero-indexed; that is, referring to the array initialization above, the first element of the array is at index 0.
Using a Motor Shield
- Connect the + and – of your battery to Vin and GND, respectively.
- Connect the two wires sticking out of each motor to + and –. Note: There are two channels, and you can connect two motors, namely motor A and motor B.
To turn on motor A, send a PWM to the pin labeled as PWM A. Send a HIGH or LOW to the pin labeled as DIR A to control the direction of its movement. The same applies to motor B.
All the analog input pins and the digital pins numbered 0, 1, 2, 4, 5, 6, 7, and 10 are free to be connected to sensors and other motor(s). Do not use the brake pins 8 and 9.
Ultrasonic Sensor (pingPin
)
pingPin
= the digital pin to which the sensor signal is connected.
pulseIn(pingPin, HIGH)
: an Arduino built-in function that returns a long integer value, which is the number of microseconds until the pin pingPin
goes HIGH.
Signaling Sequence
- Send a LOW to
pingPin
. - Wait for 2μs.
- Send a HIGH to
pingPin
. - Wait for 5μs.
- Send a LOW to
pingPin
. Duration = pulseIn(pingPin, HIGH);
- Convert the duration to distance based on the speed of sound.
Pull-up and Pull-down Resistors
Pull-up Resistor
- Push button pressed: The input to the processor is logical 0.
- Push button not pressed: The pull-up resistor allows the input to the processor to become logical 1.
Pull-down Resistor
- Push button pressed: The input to the processor is logical 1.
- Push button not pressed: The pull-down resistor allows the input to the processor to become logical 0.
Motors
Lorentz’s Force Law
When a wire through which an electric current flows is exposed to a magnetic field, a force is exerted on the current-carrying wire.
Commutator with Brushes
Brushes in early motors consisted of bristles of copper wire flexed against the commutator. Now they are usually made of graphite, which provides a larger contact area and is self-lubricating. The brushes are usually spring-loaded to ensure continual contact with the commutator.
Brushless DC Motor
A brushless DC motor uses proximity sensors and circuitry, instead of mechanical and electrical contact, to effect switching of coil current directions.
Servomotor
Closed-loop control with feedback: requires an output speed and/or torque sensor to feed back output values to continuously compare the actual output to a desired value, called the set point. The controller then actively changes the motor output to move closer to the set point.
Stepper Motors
Steppers are motors that rotate a specific number of degrees in response to control pulses. The number of degrees in each step is motor-dependent, ranging from 1-30 degrees per step. Steppers commonly used with Arduino:
- Bipolar (typically with four leads attached to two coils)
- Unipolar