Arduino Exercises PDF

Document Details

Ahmedabad University

Ashok Ranade/Maryam Kaveshgar

Tags

arduino electronics programming microcontroller

Summary

This document is a set of notes on Arduino exercises. It covers topics such as block diagrams, software development, input/output pins, and functions. The document also includes exercises and examples related to analog-to-digital conversion (ADC), pulse-width modulation (PWM), and control of duty cycle.

Full Transcript

ARDUINO EXERCISES ENR107 ASHOK RANADE/MARYAM KAVESHGAR BLOCK DIAGRAM Analog Analog input output Sensor 1 Output Computer device1...

ARDUINO EXERCISES ENR107 ASHOK RANADE/MARYAM KAVESHGAR BLOCK DIAGRAM Analog Analog input output Sensor 1 Output Computer device1 Board Sensor 2 44.2 Analog Digital Input Output Development of software Software written on a general purpose computer Uploaded on the control board After this the control board can be used as a stand alone board for some application We will study Arduino board USB Connector Battery Connector Arduino UNO is Input/Output Pins a microcontroller board based on Analog Input (6 pins) the ATmega328P. Any value between Vmin to Vmax For Arduino : 0 to 5 volts Digital pins (14) Digital Input : 0 or 5 volts Digital Output : 0 or 5 volts PWM output (6) Analog output Arduino Syntaxes https://www.arduino.cc/reference/en/ Analog to digital converter Computer uses numbers in binary form A/D converter converts analog input into a multibit binary number In Arduino it is 10 bit number So 0 volts Corresponds to binary 0000000000 decimal 0 5 volts correspond to binary 1111111111 decimal 1023 Sampling rate of ADC in Arduino Sampling rate = 9650 samples/Sec (Baud Rate) Maximum theoretical frequency of the input analog signal is half of this Other rates are also possible with advanced programming PWM waveform Tw Duty Cycle = ×100 % T 5 × Tw Average Value = T 5 v Tw T t 1 Frequency = = 480 Hz. T Other values are also possible Control of duty cycle PWM : Duty cycle 0% for 00000000 (Decimal 0) 100 % for 11111111 (Decimal 255) Program structure Declarations Digital Read function Syntax digitalRead(pin) Description Reads the value from a specified digital pin, either HIGH or LOW. x = digitalRead(Inpin) Class Exercise 4.1 Write a program to sense a slide switch. If it is connected to +5 , led connected to pin 12 flashes periodically. Otherwise the led remains off. Color code for circuits Positive supply voltage1 -------- RED Ground ---------GREEN Positive supply voltage2 -------- Pink Negative supply voltage ------BLACK Input lines --------BLUE Output lines -----PURPLE Multimeter (Or CRO) +ve terminal ------ YELLOW Lay out Use bus lines for supply voltages Connecting lines should be either horizontal or vertical A neat layout Use comments Tinkercad simulator Analog Read and Serial Communication analogRead(potPin) This will read the analog voltage from the input pin x = analogRead(potPin); (10 input pins) x ranges from 0 to 1023 Serial.begin(9600) // opens serial port, sets data rate to 9600 bps (Baud rate) This will establish serial communication between the computer and Arduino. This is in the set-up section. Serial.println("ADC output"); Serial.println(x); Digital/Analog Conversion Calculation 5 Analog Value = × Digital value 1023 Digital to analog conversion Digital Value Analog Value 6 0 0 100 0.488758553 5 200 0.977517107 4 300 1.46627566 400 1.955034213 Analog 3 Value 500 2.443792766 Series1 2 600 2.93255132 700 3.421309873 1 800 3.910068426 900 4.398826979 0 0 200 400 600 800 1000 1200 1023 5 Digital Value An Exercise 4.2 (analogRead( )) A potentiometer is used to generate a voltage varying between 0 and 5 volts. The output of the potentiometer is connected to one analog input pin of Arduino. This voltage is measured by a meter. Write a program to read the output of the potentiometer. As pot output is varied from 0 to 5 volts, the value read varies from 0 to 1023. Next the digital value is converted to analog by using the equation. Both values are displayed on monitor. The analog value should match that of meter Circuit and program Program void loop() int Vi; { float Vo; Vi = analogRead(A1); void setup() Vo = (5.0/1023.0)*Vi; { Serial.println(“Digital Value"); Serial.begin(9600); Serial.println(Vi); } Serial.println(); Serial.println(“Analog Value"); Serial.println(Vo); Serial.println(); delay(2000); } analogWrite function Certain digital pins can be used as PWM pins analogWrite( pin number, value); value is an integer from 0 to 255 Decides the duty cycle of the PWM waveform value 0 means 0% duty cycle 255 means 100% duty cycle PWM waveform (Pulse Width Modulation) Tw Duty Cycle = ×100 % T 5 v Tw T t 5 × Tw 1 Average Value = Frequency = = 480 Hz. T T Other values are also possible Value and average value Value Average Value 6 0 0 5 25 0.490196078 50 0.980392157 4 75 1.470588235 Average 3 Value 100 1.960784314 2 125 2.450980392 1 150 2.941176471 0 175 3.431372549 0 50 100 150 200 250 300 200 3.921568627 Value 225 4.411764706 255 5 Average value analogWrite( pin number, value); 5 × Tw Average value = T 5 × value Average value = 255 Average calculation Examples 5 Average value = × 31 = 0.6 volt 255 5 Average value = ×127 = 2.49 volts 255 Exercise Interface a potentiometer to the Arduino such that voltage input to an analog pin can be varied from 0 to 5 volts. Connect the output of a PWM pin to a Scope to measure its frequency. Also connect the output of the PWM pin to a low pass RC filter. Select R and C such that its time constant is much larger than the time period of PWM waveform. This filter will extract the dc output. Note that PWM frequency is about 500 Hz on some pins and about 1000 Hz on some other pins Program Write a program such that as the input voltage to the analog pin is varied from 0 to 5 volts the duty cycle of the PWM waveform varies from 0 to 100% Execute the program and (1) Measure the frequency (2) Verify that as the pot is varied the duty cycle indeed varies from 0 to 100 % (3) Measure the dc voltage at the output of the filter Circuit R = 10 Kohms C = 10 Microfarads Code Animation

Use Quizgecko on...
Browser
Browser