Presentation is loading. Please wait.

Presentation is loading. Please wait.

Microcontroller basics

Similar presentations


Presentation on theme: "Microcontroller basics"— Presentation transcript:

1 Microcontroller basics
Embedded systems for mortals

2 Analog input Interrupts Basic serial
Lesson 2 Analog input Interrupts Basic serial

3 Analog input in Uno Multiple voltages can be read between 0-5V
Input voltage is compared to a reference voltage By default the reference voltage has to be connected to AREF-pin 10 bit conversion

4 How A/D conversion works
With 10 bits you can have 210 = 1024 different values Linearly scaled 0V = 0 | 2,5V = 512 | 5V = 1023

5 Potentiometer Linear and Logarithmic

6 Example 4 – Breadboard Setup

7 Example 4 – Potentiometer
Basic A/D byte analogPin = A0; //Analog pin A0 byte ledPin = 13; //A byte stores an 8-bit unsigned number, from 0 to 255. int value = 0; //can store value from -32,768 to 32,767 void setup() { pinMode(analogPin, INPUT); pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW); } void loop() value = analogRead(analogPin); //Read analog value digitalWrite(ledPin, HIGH); delay(value); It is more efficient memory wise to use small types (e.g.) byte. What does the code do? What is the max. delay?

8 Arduino C – Analog functions
analogRead(var1) analogRead function performs an analog reading operation on a selected pin. Var1 is the number of the analog pin. analogWrite(var1, var2) analogWrite can be used on PWM pins to generated a square wave. Var1 is the number of the pin and var2 is the value (0-255) analogReference(var1) analogReference is used to change the source of reference voltage for ADC. Var1 defines the source: (DEFAULT, INTERNAL or EXTERNAL) IMPORTANT TO NOTICE: These analog functions are generic functions that are supposed to work on all Arduino boards. The actual effect might not be what you initially expect! Using analogRead will force the reference voltage to be whatever has been set with analogReference -function

9 Interrupts There are both internal and external interrupts
Internal interrupts Generated by timers, communication protocols, ADC and many other internal components External interrupts Generated by changes in certain pins

10 Interrupts When an interrupt is detected the processor moves directly to execute the code related to given interrupt After the interrupt routine is completed the processor returns to it’s previous position before the interrupt occurred

11 Example 5 – Breadboard Setup

12 Example 5 - Interrupt External interrupt
byte analogPin = A0; byte ledPin = 13; byte interruptPin = 2; //External interrupt pin D0 int value = 0; //can store value from -32,768 to 32,767 void interruptFun() { digitalWrite(ledPin, HIGH); } void setup() pinMode(analogPin, INPUT); pinMode(ledPin, OUTPUT); pinMode(interruptPin, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(interruptPin), interruptFun, FALLING); void loop() value = analogRead(analogPin); //Read analog value if(digitalRead(ledPin)) //if this statement is true (= meaning HIGH) delay(value); digitalWrite(ledPin, LOW); digitalPinToInterrupt(interruptPin) is used so that the pin is assingened correctly. More info: (In uno the pins 2 and 3 are interrupts which need to be called with 0 and 1, respectively.

13 Arduino C – Interrupt functions
attachInterrupt(var1, var2, var3) attachInterrupt creates a connection with external interrupt source and a function. Var 1 is the number of the external interrupt pin, var 2 is the name of the function to call when interrupt occurs and var3 is the mode of operation: (LOW, CHANGE, FALLING or RISING) detachInterrupt(var1) Opposite of attachInterrupt. Brakes the connection between external interrupt pin and a function. Var1 is the number of the external interrupt pin. Test out different var3 values on the attachInterrupt function. What happens? IMPORTANT TO NOTICE: These functions only work for external interrupts. Internal interrupts have to be programmed using the registers, timers and basic C –code. Uno Board Digital Pins Usable For Interrupts: 2, 3 (which are referred as 0 and 1, respectively)

14 Serial communication over USB
Can be used for multiple things Communicating with computer programs Debugging Requires the use of Serial library Serial library is automatically included when Serial.begin() -function is called

15 Example 6 - Serial Using Serial communication over USB
byte ledPin = 13; void setup() { pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW); Serial.begin(9600); //Begin serial communication with baud rate 9600 } void loop() digitalWrite(ledPin, HIGH); Serial.println(“LED ON”); //Send a line of text to serial port delay(1000); Serial.println(“LED OFF”);

16 Arduino C – Serial functions
Serial.begin(baud, config) Starts the serial communication over the USB. Baud is the desired speed of communication (baud rate) and config is the configuration mode (optional). Serial.println(”text”) Serial.println(var) Print a line to the serial port. After printing send a new line character (“\n”). With quotes it sends text and without, it prints the contents of the variable. Serial.print(”text”) Serial.print(var) Same as the Serial.println() except it doesn’t send the new line character. MORE INFO:

17 Bonus Round! Add a serial command to the example 5 – interrupt to print out the LEDs time [s] and the voltage signal of the potentiometer [V] Add 3 LEDs to the breadboard (as learned in lesson 1) and make a sequence that is repeated in void loop(). Decide what happens with an interrupt and what is the effect of the potentiometer.

18 Serial communication between devices LCD screen Sensors
In next lesson Serial communication between devices LCD screen Sensors


Download ppt "Microcontroller basics"

Similar presentations


Ads by Google