Microcontroller Programming & Hardware Introduction

Post on 13-May-2015

372 views 14 download

Tags:

description

Introduction to microcontrollers namely and more specifically, Arduino. This course is also an introduction to hardware.

Transcript of Microcontroller Programming & Hardware Introduction

WELCOME TO MICRO-CONTROLLER

PROGRAMMING & HARDWARE

INTRODUCTION

With this class, you will begin learning how to create custom software defined

hardware devices and start building your own electronic devices

Brought To You By Your Local Make

Space

www.MakeSpace.io

Class Instructor

Joseph Sanchez

6/24/2014

FIRST, LET’S LOOK AT

WHAT KIND OF THINGS

ONE MAY BE ABLE TO

CREATE WITH THESE

SOON- TO-BE LEARNED

ABILITIES…

Make these soon one might

ONE MIGHT….

build cool robots!

make an automated beverage dispenser

ONE MIGHT….

build an automated watering system for plants

ONE MIGHT….

Build a digital harp that uses lasers as strings,a techno-inspired led helmet and more!

ONE MIGHT….

MICRO-CONTROLLERS, PROGRAMMING AND ADDITIONAL HARDWARE WILL HELP YOU IN THE CREATION OF ALL OF THESE DEVICES AND MORE!

Wouldn’t those type of abilities make you happy?

GREAT LET’S START LEARNING!

GETTING STARTED THE BASICS

WHAT IS A MICROCONTROLLER

A microcontroller (sometimes abbreviated µC, uC or MCU) is a small computer on a single integrated circuit containing a processor core, memory, and programmable inputs and outputs. Program memory is also often included on chip, as well as a typically small amount of RAM.

SO, WHAT IS AN ARDUINO? Arduino uses a microcontroller. It often referred to as a development

board. It is an open-source electronics

prototyping platform based on flexible, easy-to-use hardware and software. It's intended for artists, designers and hobbyists.

OTHER DEV BOARD OPTIONS There additional options for

development boards other than Arduino like…

Raspberry Pi Beagle

……And Many More!

BUT FOR NOW,WE WILL JUST BE FOCUSING ON THE ARDUINO

The internal voltage reference options may not be used if an external reference voltage is being applied to the AREF pin.

Arduino

GETTING TO KNOW THE BOARD

TX/RX more on these communicationports a little later

GETTING TO KNOW THE BOARD

Rx = (received data)Tx = (transmit data)

These additional input and output ports are for serial data.

They allow you to do things like connect two Arduinos together

so they can communicate!

COMMUNICATION PORTS

SO WHAT KIND OF DEVICES COULD WE USE WITH OUR INPUTS

AND OUTPUTS?

Gadgets, sensors and motors oh

my!

HARDWARE COMES IN AN ASSORTMENTOF FLAVORS… There is already a ton of sensors, motors, and other

electrical devices to use in combination with your Arduino

These additional devices help our devices see, feel, measure, manipulate and hear our surroundings.

The more information you know about the availability of these devices, the better you can create a solution for the task you are trying to accomplish with your Arduino.

HERE IS A SHORT LIST OF

HARDWARE TO START BUILDING

YOUR KNOWLEDGE

BASE…

LED L.E.D which stands for light emitting diode. Is a diode that illuminates when current is applied through it.

RESISTOR Resistors come in a variety of sizes and they are used to reduce current flow, and, at the same time, act to lower voltage levels within circuits.

Schematic Representations of Resistors

RELAY

Relays are devices which allow us to complete or disconnect a circuit based on an input voltage. Based on the rating of the relay, you can switch a variety of voltages and amperages.

Relays could be used to :

-Turn on water pumps-Turn off lights or appliances in your house

ELECTRIC MOTOR

Electric motors spin when voltage is applied.

If you reverse the power in (most) motors, the motor direction will reverse.

Take for example our 3D printers – They require to go in one direction and then reverse to control the printing.

SERVO MOTORS

Servo motors move similarly, but they allow you to control how they turn more specifically. They use degrees or values which may be translated into degrees allowing the gear to rotate.

For instance, you could rotate 45 degrees with one command or even a 360 degree turn as well as everything in between.

PHOTOCELLS OR PHOTORESISTOR

Photocells are variable resistors where the resistance decreases with the increasing amount of light you shine onto it.

A great example of using a photocell would be a laser trip wire. In order to achieve this, we would place a laser towards the photocell and if something would come in between the two, the resistance would increases. By doing so, we could perform an action like sounding An alarm.

HUMIDITY SENSOR

Humidity sensors do just as they sound, they detect the levels of humidity in the air. So, depending on the level of moisture in the air, a value decreases or increases.

POTENTIOMETERPotentiometers are devices that use variable resistors along with a turning mechanism to increase or decrease resistance.

In order to achieve this, you would add a positive connection to left connection(1) and ground to the right(3). Then, as you twisted the knob, the middle connection’s value(2) would increase or decrease depending on the orientation of your rotation.

All the way counter clockwise = 0 All the way clockwise = 255

And anything inbetween during rotation

GYROSCOPE

Gyroscopes which have been ever increasing in their popularity due to smart phones and personal drones, allow us to sense the tilt and rotation of a device.

SO, IF YOU GET AN IDEA FOR SOMETHING TO BUILD, THERE IS A GOOD CHANCE THERE IS A PIECE

OF HARDWARE TO HELP YOU ACCOMPLISH YOUR GOAL!

And that’s just the some of the available devices……

SO HOW DO WE GET THESE DEVICES TO DO

WHAT WE WANT?

Well, that’s where the programming comes in…

I HAVE NEVER PROGRAMMED BEFORE, IS IT

EASY TO LEARN?

Well luckily, you have come to the right place!

Today, you will get hands on experience to get you started using the Arduino and by the end of this class you will have successfully written your first few programs.

SO HOW DO YOU PROGRAM AN ADRUINO?

We will be using the an IDE specifically made for programming our Adruinos.

IDE stands for integrated development environment.

This is where we will write the code that is stored and ran on our microcontroller.

NOW, LET’S CODE OUR FIRST PROGRAM!

SETTING UP OUR PROGRAM

void setup() {

}

Whatever code you add between the curly brackets of void setup will be ran prior to the main program.

In our development window, type the following code:

YOU CAN THINK OF THE LOOP AS THE MAIN STAGE OR MAIN PROGRAM AREA

void loop() {

}

Void loop is where your main program lives. It is aptly named loop because that’s exactly what happens when it reaches the end of the code – it will LOOP and start over .

Next, under void setup type the following code:

void setup() {

}

void loop() {

}

Before the setup, we can call in Libraries which are often times pre-written code to help us with what we are trying to achieve.

It is stored as an external file and then called into the program.

An example of calling in the SD library which helps with processing an SD card would show as:

#SD.H

A QUICK MENTION ABOUT LIBRARIES

void setup() {

}

void loop() {

blinkLED(); }

After the loop, we can add functionsFunctions are ways to migrate code outside of the main program. Then, you call them independently into the code where needed. You always precede a function with void.

Now type out the code.

And then call the function blinkLED into the loop by adding:

blinkLED();

No void needed when adding the function reference

void blinkLED() {

}

NOW ADD A FUNCTION CALLED BLINKLEDAND PLACE A REFERENCE

TO IT WITHIN YOUR MAIN PROGRAM

digitalWrite is used to set the state of a pin to HIGH or LOW (or 0 -255 if using PWM)

So write the following in your blinkLED function:

digitalWrite(8, HIGH); delay(1000);

This would make pin 8 turn “on” which would illuminate an LED connected to the pin

Then add digitalWrite(8,LOW);delay(1000);

directly below it. This would produce the opposite result and turn off the connected LED

NOW LET’S ADD CODE INTO OUR FUNCTION

NOW SET PIN 8 TO OUTPUT

In order to let the program know that we are using pin 8 as an output instead of an input add the following code within the setup()

pinMode(8,OUTPUT);

SO, WHEN YOU PUT IT ALL TOGETHER IT SHOULD LOOK LIKE THIS

NOW LET’S HOOK UP OUR HARDWARE AND TRY OUR CODE!

AFTER ALL, THIS ISN’T CALLED

FOR NOTHING!