Unit Iv
Unit Iv
Unit Iv
GALILEO/ARDINO
Writing the Python Software to blink the LED
With the circuit created we need to write the Python script to blink the LED.
Before we start writing the software we first need to install the Raspberry Pi
GPIO Python module. This is a library that allows us to access the GPIO port
directly from Python.
To install the Python library open a terminal and execute the following
$ sudo apt-get install python-rpi.gpio python3-rpi.gpio
With the library installed now open your favorite Python IDE (I recommend
Thonny Python IDE more information about using it here).
Our script needs to do the following:
• Initialize the GPIO ports
• Turn the LED on and off in 1 second intervals
To initialize the GPIO ports on the Raspberry Pi we need to first import the
Python library, the initialize the library and setup pin 8 as an output pin.
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
from time import sleep # Import the sleep function from the time
module
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW) # Set pin 8 to be an
output pin and set initial value to low (off)
Next we need to turn the LED on and off in 1 second intervals by
setting the output pin to either high (on) or low (off). We do this inside a
infinite loop so our program keep executing until we manually stop it.
while True: # Run forever
GPIO.output(8, GPIO.HIGH) # Turn on
sleep(1) # Sleep for 1 second
GPIO.output(8, GPIO.LOW) # Turn off
sleep(1) # Sleep for 1 second
Combining the initialization and the blink code should give you the following
full Python program:
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
from time import sleep # Import the sleep function from the time module
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(8, GPIO.OUT, initial=GPIO.LOW) # Set pin 8 to be an output pin
and set initial value to low (off)
while True: # Run forever
GPIO.output(8, GPIO.HIGH) # Turn on
sleep(1) # Sleep for 1 second
GPIO.output(8, GPIO.LOW) # Turn off
sleep(1) # Sleep for 1 second
With our program finished, save it as blinking_led.py and run it either inside
your IDE or in the console with:
$ python blinking_led.py
intel Galileo
Digital pins 0 to 13 (and the adjacent AREF and GND pins), Analog
inputs 0 to 5, the power header, ICSP header, and the UART port pins (0
and 1), are all in the same locations as on the Arduino Uno R3.
The Arduino Cloud Editor is hosted online, therefore it will always be up-
to-date with the latest features and support for all boards. Follow
this simple guide to start coding on the browser and upload your sketches
onto your board.
Use your Intel® Galileo Gen2 on the Arduino Desktop IDE
• If you want to program your Intel® Galileo Gen2 while offline you
need to install the Arduino Desktop IDE and add the Intel® Galileo
Gen2 Core to it.
You should see a Transfer complete message when it has uploaded. Now your built-in
led should blink.
You have successfully set up your Intel® Edison board and uploaded your first sketch
1. - ARDUINO UNO
∙ Arduino is an open source advancement prototyping platform which depends on
simple to utilize equipment and programming.
∙ Arduino is a basic single board microcontroller designed to make applications,
interactive controls, or environments easily adaptive.
∙ Arduino boards are able to read inputs - light on a sensor, a finger on a button, or a
Twitter message
- and turn it into an output - activating a motor, turning on an LED, publishing something
online.
∙ The hardware consists of a board designed around an 8-bit microcontroller, or a 32-bit
ARM (Advanced RISC Machines).
∙ Arduino Uno is a microcontroller board based on the ATmega328P.
∙ It has 14 digital input/output pins (of which 6 can be used as Pulse Width Modulation
(PWM) outputs), 6 analog inputs, a 16 MHz quartz crystal, a USB connection, a power
jack, an In Circuit Serial Programming (ICSP) header and a reset button.
∙ "Uno" means one in Italian and was chosen to mark the release of Arduino Software
(IDE) 1.0.
∙ The Uno board and version 1.0 of Arduino Software (IDE) were the reference versions
of Arduino, now evolved to newer releases.
1. Why Arduino?
❖ Inexpensive:
o Arduino boards are relatively inexpensive compared to other microcontroller
platforms.
o The least expensive version of the Arduino module can be assembled by hand.
❖ Cross-platform:
o The Arduino software runs on Windows, Macintosh OS and Linux operating systems.
❖ Simple, clear programming environment:
o The Arduino programming environment is easy-to-use for beginners, yet flexible
enough for advanced users to take advantage of as well.
❖ Open source and extensible software:
o The Arduino software is published as open source tools, available for extension by
experienced programmers. The language can be expanded through C++ libraries.
o The Arduino is based on Atmel's ATMEGA microcontrollers.
o Even relatively inexperienced users can build the breadboard version of the module
in order to understand how it works and save money.
❖ Entry Level
o Easy to use and ready to first creative projects.
o These boards and modules are the best to start learning and tinkering with
electronics and coding.
❖ Enhanced Features
o Experience the excitement of more complex projects, with advanced functionalities,
Which Arduino?
• Over ten years, since Arduino released, hundreds of Arduino boards are available in
the market.
• Out of which Arduino UNO which is used in almost 99% of project use.
• Some of the boards from Arduino family are
– ARDUINO UNO
• A microcontroller board based on the ATmega328P.
• It has 14 digital input/output pins (of which 6 can be used as PWM outputs), 6 analog inputs, a 16
MHz quartz crystal, a USB connection, a power jack, an ICSP header and a reset button.
• Connect it to a computer with a USB cable or power it with a AC-to-DC adapter or battery to get
started.
– ARDUINO MEGA 2560
• A microcontroller board based on the ATmega2560.
• It has 54 digital input/output pins (of which 15 can be used as PWM outputs), 16 analog inputs, 4
UARTs (hardware serial ports), a 16 MHz crystal oscillator, a USB connection, a power jack, an ICSP
header, and a reset button.
• It is the recommended board for 3D printers and robotics projects.
– ARDUINO MICRO
• A microcontroller board based on the ATmega32U4, featuring a built-in USB which makes the Micro
recognizable as a mouse or keyboard.
•
It has 20 digital input/output pins (of which 7 can be used as PWM outputs and 12 as analog inputs),
a 16 MHz crystal oscillator, a micro USB connection, an ICSP header, and a reset button.
– ARDUINO MKR1000
• It is based on the Atmel ATSAMW25 ARM SoC (System on Chip), that is part of the Smart Connect
family of Atmel Wireless devices, specifically designed for IoT projects and devices.
• The ATSAMW25 is composed of three main blocks:
– SAMD21 Cortex-M0+ 32bit low power ARM MCU
– WINC1500 low power 2.4GHz IEEE® 802.11 b/g/n Wi-Fi
– ECC508 Crypto Authentication
– PCB Antenna.
•
ARDUINO
EXPLORING ARDUINO UNO LEARNING BOARD
1. Reset Button This will restart any code that is loaded to the Arduino board
2. AREF Stands for “Analog Reference” and is used to set an external reference voltage
3. Ground Pin There are a few ground pins on the Arduino and they all work the same
4. Digital Input / Output Pins 0-13 can be used for digital input or output
5. PWM The pins marked with the (~) symbol can simulate analog output
6. USB Connection
Used for powering up your Arduino and uploading sketches
7. TX/RX Transmit and receive data indication LEDs
8. ATmega Microcontroller
This is the brains and is where the programs are stored
9. Power LED Indicator
This LED lights up anytime the board is plugged in a power source
10. Voltage Regulator
This controls the amount of voltage going into the Arduino board
11. DC Power Barrel Jack
This is used for powering your Arduino with a power supply
12. 3.3V Pin
This pin supplies 3.3 volts of power to your projects
13. 5V Pin
This pin supplies 5 volts of power to your projects
14. Ground Pins
There are a few ground pins on the Arduino and they all work the same
15. Analog Pins
These pins can read the signal from an analog sensor and convert it to digital
Installing the Software (Arduino IDE)
• The Arduino Software (IDE) allows you to write programs and upload
them to board.
• In the Arduino Software page, you will find two options:
– Online IDE (Arduino Web Editor). It will allow to save sketches in the cloud,
having them available from any device and backed up.
– Offline, should use the latest version of the desktop IDE.
•
• Install the Arduino Desktop IDE accordingly to operating system.
– Windows
– Mac OS X
– Linux
– Portable IDE (Windows and Linux)
•
• Choose board in the list here on the right to learn how to get started with
it and how to use it on the Desktop IDE.
Fundamentals of Arduino
Programming:
Two required functions / methods / routines:
void setup()
{
// runs once
}
void loop()
{
// repeats
}
Connecting Arduino UNO Learning Board
– If you want to program your Arduino Uno while offline, you need to install
the Arduino Desktop IDE.
– Connect your Uno board with a USB cable; sometimes this cable is called a
USB printer cable.
– If you used the Installer, Windows - from XP up to 10 - will install drivers
automatically as soon as you connect your board.
– You need to select the entry in the Tools > Board menu that corresponds to
your Arduino or Genuino board. (Figure shows layout of Arduino IDE)
– Select the serial device of the board from the Tools | Serial Port menu.
– This is likely to be COM3 or higher (COM1 and COM2 are usually reserved
for hardware serial ports).
– To find out, you can disconnect your board and re-open the menu; the
entry that disappears should be the Arduino or Genuino board.
– Reconnect the board and select that serial port.
Open your first sketch.
Open the LED blink example sketch: File > Examples >01.Basics > Blink.
Upload the program
Now, simply click the "Upload" button in the environment. Wait a few seconds -
you should see the RX and TX leds on the board flashing. If the upload is
successful, the message"Done uploading." will appear in the status bar.
∙ A few seconds after the upload finishes, you should see the pin
13 (L) LED on the board start to blink (in orange).
∙ Layout of Arduino Uno IDE
1. Fundamentals of Arduino Programming
• The Arduino IDE supports the languages C and C++ using special rules of code structuring.
• The Arduino IDE supplies a software library from the Wiring project, which provides many common input and
output procedures.
• User-written code only requires two basic functions, for starting the sketch and the main program loop, that are
compiled and linked with a program stub main() into an executable cyclic executive program with the GNU
toolchain, also included with the IDE distribution.
• Structure: Structure of Arduino programming contains two part
Setup()
This function is called once when a sketch starts after power-up or reset.
It is used to initialize variables, input and output pin modes, and other libraries needed in
the sketch
void setup ( ) //after calling setup(), loop() function dos its task
{
digitalWrite(pin, HIGH); //set pin ON
delay(10000); // pauses for ten thousand milliseconds
digitalWrite(pin, LOW); //set pin OFF
delay(10000); // pauses for ten thousand milliseconds
}
Loop()
After setup() function exits (ends), the loop() function is executed repeatedly in the main
program.
It controls the board until the board is powered off or is reset.
void loop( ) //after calling setup(), loop() function does its task
{
digitalWrite(pin, HIGH); //set pin ON
delay(10000); // pauses for ten thousand milliseconds
digitalWrite(pin, LOW); //set pin OFF
delay(10000); // pauses for ten thousand milliseconds
}