Arduino Hands-On
Arduino Hands-On
OBJECTIVES
• To build basic Tinkercard circuits.
• To create elementary Arduino circuits.
• To manage electronic elements as resistors, leds, breadboards,…
• To follow the sequence of instructions of a basic program and preview
the output results.
• To read and understand initial C++ programs.
• To write short and basic C++ programs using the most elementary
structures.
ARDUINO BASIC – LED Programming
Arduino Code:
Note:
Arduino output when high is 5V. The led, when lighting has a Vled near 2V, so Vr=5V-Vled=3V. If we
want the led shines properly we will use a current of 15mA, so R=3V/15mA = 200ohm.
Arduino Code:
Arduino Code:
OBJECTIVES
Serial.begin(rate)
Opens serial port and set baud rate for serial data trasmission. Typically baud rate for
communication with computer is 96000 although other speeds are supported.
Note: when using serial communication digital pins 0(RX) and 1(TX) cannot be used at the same
time
Serial.println(data)
Print data to the serial port followed by an automatic carriage return and line feed .This
command takes the same form as Serial.print(), but is easier for reading data on the Serial
monitor.
Arduino Code:
Connect three wires to the Arduino board. The first goes to ground from one of the outer pins of
the potentiometer. The second goes from 5 volts to the other outer pin of the potentiometer.
The third goes from analog input 2 to the middle pin of the potentiometer.
By turning the shaft of the potentiometer, it change the amount of resistence on either side of
the wiper which is connected to the center pin of the potentiometer. This changes the relative
"closeness" of that pin to 5 volts and ground, giving us a different analog input. When the shaft
is turned all the way in one direction, there are 0 volts going to the pin, and we read 0. When the
shaft is turned all the way in the other direction, there are 5 volts going to the pin and we read
1023. In between, analogRead() returns a number between 0 and 1023 that is proportional to
the amount of voltage being applied to the pin.
Arduino Code:
When the pushbutton is open (unpressed) there is no connection between the two legs of the
pushbutton, so the pin is connected to ground (through the pull-down resistor) and read as LOW.
When the button is closed (pressed), it makes a connection between its two legs, connecting the
pin to 5 volts, so that read as HIGH.
If it disconnect the digital I/O pin from everything, the LED may blink erratically. This is because
the input is "floating" that is, it will randomly return either HIGH or LOW. That's why it need a
pull-up or pull-down resistor in the circuit.
Arduino Code:
Arduino Code:
tone()
Generates a square wave of the specified frequency (and 50% duty cycle) on a pin. A duration can
be specified, otherwise the wave continues until a call to noTone(). The pin can be connected to a
piezo buzzer or other speaker to play tones. Only one tone can be generated at a time. If a tone is
already playing on a different pin, the call to tone() will have no effect. If the tone is playing on the
same pin, the call will set its frequency.
Arduino Code:
Arduino Code:
The Arduino board sends a short pulse to trigger the detection, then listens for a pulse on the
same pin using the pulseIn() function. The duration of this second pulse is equal to the time taken
by the ultrasound to travel to the object and back to the sensor. Using the speed of sound, this
time can be converted to distance.
For example, if the object is 10 cm away from the sensor, and the speed of the sound is 340 m/s
or 0.034 cm/µs the sound wave will need to travel about 294 u seconds. But what it will get from
the Echo pin will be double that number because the sound waves need to travel forward and
bounce backward. In order to get the distance in cm it need to multiply the received travel time
value from the echo pin by 0.034 and divide it by 2.
The PIR sensor itself has two slots in it, each slot is made of a special material that is sensitive
to IR. The lens used here is not really doing much and so we see that the two slots can 'see' out
past some distance (basically the sensitivity of the sensor). When the sensor is idle, both slots
detect the same amount of IR, the ambient amount radiated from the room or walls or outdoors.
When a warm body like a human or animal passes by, it first intercepts one half of the PIR sensor,
which causes a positive differential change between the two halves. When the warm body leaves
the sensing area, the reverse happens, whereby the sensor generates a negative differential
change. These change pulses are what is detected.
Arduino Code: