arduino examples

Download Arduino Examples

If you can't read please download the document

Upload: carlos-deza

Post on 14-Sep-2015

20 views

Category:

Documents


2 download

DESCRIPTION

some arduino examples to build.

TRANSCRIPT

Frequency counter using Arduino (upto 40KHz).

Many guys here were asking for a frequency counter and at last I got enough time to make one. This frequency counter using arduino is based on the UNO version and can count up to 40KHz. A 162 LCD display is used for displaying the frequency count. The circuit has minimum external components and directly counts the frequency. Any way the amplitude of the input frequency must not be greater than 5V. If you want to measure signals over than 5V, additional limiting circuits have to be added and i will show it some other time. Now just do it with 5V signals.

The frequency to be counted is connected to digital pin 12 of the arduino. pulseIn() function is used here for counting the frequency connected to pin 12. pulseIn() function counts the number of pulses (HIGH or LOW) coming to a particular pin of the arduino. The general syntax of this function is pulseIn(pin, value, time) where pin is the name of the pin, value is either HIGH or LOW and time is time for which the function to wait for a pulse. The function returns zero if there is no valid pulse with in the specified time. The pulseIn() function can count pulses with time period ranging from 10 S to 3 minutes. Circuit diagram of the frequency counter using arduino is given below.

Potentimeter R1 is used to adjust the contrast of the LCD screen. Resistor R2 limits the current through the back light LED.

In the program, high time and low time of the input signal is measured using separate pulseIn() functions. Then the high and low times are added together to get the total time period of the signal. Frequency is just 1/time period in seconds. The pulseIn() function returns the time period in microseconds. Total timeperiod in microseconds first divided by 1000. Then 1000 is divided by the result to get the frequency in hertz. The program of the frequency counter using arduino is shown below.

Program.

#include int input=12;

int high_time;int low_time;float time_period;float frequency;LiquidCrystal lcd(7, 6, 5, 4, 3, 2);void setup(){pinMode(input,INPUT);lcd.begin(16, 2);}void loop(){lcd.clear();lcd.setCursor(0,0);lcd.print("Frequency Meter");

high_time=pulseIn(input,HIGH);low_time=pulseIn(input,LOW);

time_period=high_time+low_time;time_period=time_period/1000;frequency=1000/time_period;lcd.setCursor(0,1);lcd.print(frequency);lcd.print(" Hz");delay(500);}

The circuit can be powered through the 9V external power jack of the arduino. 5V DC required at some parts of the circuit can be tapped from the built in 5V regulator of the arduino itself. This is actually a simple counter circuit using arduino. We can modify this circuit for other applications like tachometer, intrusion counter etc.

PWM motor speed control using Arduino.

PWM or pulse width modulation is a very common method used for controlling the power across devices like motor, light etc. In PWM method the power across the load is controlled by varying the duty cycle of the drive signal. More the duty cycle more power is delivered across the load and less the duty cycle, less power is delivered across the load. A hex keypad is used for controlling the speed. The speed can be varied in seven steps using the hex keypad. Arduino UNO is the type os arduino development board used in this circuit. The circuit diagram of the PWM motor speed control using arduino is shown in the figure below.

Circuit diagram.

pwm motor speed control using arduinoRow pins R1 and R2 of the hex keypad are interfaced to digital pins 6 and 7 of the arduino. Column pins C1, C2, C3 and C4 are interfaced to the digital pind 10, 11, 12 and 13 of the arduino. The key pressed on the hex keypad is identified using the column scanning method and it is explained in detail in this article. Interfacing hex keypad to arduino. The digital pins of the arduino can source or sink only up to 4omA of current. So the digital pin 3 cannot drive the motor directly. To solve this problem an NPN transistor (2N2222) is used to drive the motor according the the PWM signal available at digital pin 3. 100 ohm resistor R1 is used to limit the base current of the transistor. The motor is connected as a collector load to the transistor. The 0.1uF capacitor C1 connected across the motor is used to by-pass the voltage spikes and noises produced during the switching of the motor.

The arduino board is powered through the external power jack provided on the board. The arduino board can be also powered by the PC through USB but there must be an additional external source for powering the motor. The complete program for PWM motor speed control using arduino is given below. Explanation of the program is given under the About the program heading.

Program.

int pwm=3; // declares digital pin 3 as PWM outputint r1=6;int r2=7;int c1=10;int c2=11;int c3=12;int c4=13;int colm1;int colm2;int colm3;int colm4;

void setup(){pinMode(r1,OUTPUT);pinMode(r2,OUTPUT);pinMode(c1,INPUT);pinMode(c2,INPUT);pinMode(c3,INPUT);pinMode(c4,INPUT);pinMode(pwm,OUTPUT);digitalWrite(c1,HIGH);digitalWrite(c2,HIGH);digitalWrite(c3,HIGH);digitalWrite(c4,HIGH);digitalWrite(pwm,LOW);}void loop(){digitalWrite(r1,LOW);digitalWrite(r2,HIGH);colm1=digitalRead(c1);colm2=digitalRead(c2);colm3=digitalRead(c3);colm4=digitalRead(c4);if(colm1==LOW) //checks whether key "1" is pressed. { analogWrite(pwm,42); // writes "42" (duty cycle 16%).delay(200);}else{if(colm2==LOW) //checks whether key "2" is pressed. { analogWrite(pwm,84); // writes "84" (duty cycle 32%).delay(200);}else{if(colm3==LOW) //checks whether key "3" is pressed {analogWrite(pwm,126); // writes "126" (duty cycle 48%).delay(200);}else{if(colm4==LOW) // checks whether key"A" is pressed. {digitalWrite(pwm,LOW); // makes pin 3 LOW (duty cycle 0%).Motor OFF.delay(200);}}}}

digitalWrite(r1,HIGH);digitalWrite(r2,LOW);colm1=digitalRead(c1);colm2=digitalRead(c2);colm3=digitalRead(c3);colm4=digitalRead(c4);if(colm1==LOW) // checks whether key "4" is pressed. {analogWrite(pwm,168); //writes "168" (duty cycle 64%).delay(200);}else{if(colm2==LOW) // checks whether key "5" is pressed. {analogWrite(pwm,202); // writes "202" (duty cycle 80%).delay(200);}else{if(colm3==LOW) // checks whether key "6" is pressed. {analogWrite(pwm,244); // writes "244" (duty cycle 96%).delay(200);}else{if(colm4==LOW) // checks whether key "B" is pressed. {digitalWrite(pwm,HIGH);//makes pin 3 HIGH (duty cycle 100%). FULL POWERdelay(200); }}}}}

About the program.

The duty cycle of the PWM control signal is varied by varying the value written to the output pin 3 using the analogWrite() function. The range of the value that can be written is between 0 and 255. The anlogWrite() function can be employed on pins 3, 5, 6, 9, 10 and 11 in the Arduino UNO board. In most of the arduino boards the frequency of the PWM signal will be around 490Hz. The duty cycle of the PWM signal is proportional to the value written using the analogWrite() function. Few examples using the analogWrite() function are shown below.

analogWrite(pwm,255) will generate a pwm wave of 100% duty cycle (full power) at the pin denoted by the variable pwm.

analogWrite(pwm,128) will generate a pwm wave of 50% duty cycle (half power) at the pin denote by the variable pwm.

analogWrite(pwm,0) will generate a pwm wave of 0% duty cycle (no power) at the pin denoted by the variable pwm.

In the program the digital pin 3 is configured as the PWM output pin. Keys 1 to 6 on the hex keypad are used for increasing the power in steps of 42 in terms of the value written using the analogWrite() function or 16% in terms of duty cycle. Key A on the hex keypad is used for switching the motor OFF and it is done using the command digitalWrite(pwm,LOW);. Key B on the hex keypad is used for putting the motor at maximum speed an it is done using the command digitalWrite(pwm,HIGH);.

Notes.

Instead the motor you can also use the same circuit for varying the brightness of an LED string. Any way the load current must be in the safe limits of transistor 2N2222 and it is 800mA. Also the external power supply must be powerful enough to drive the LED string. The circuit diagram of PWM brightness control of LED using arduino is shown in the figure below.

Brightness control of led using arduino:

PWM Control using Arduino Learn to Control DC Motor Speed and LED Brightness

In this article we explain how to do PWM (Pulse Width Modulation) control using arduino. If you are new to electronics, we have a detailed article explaining pulse width modulation. We have explained PWM in this tutorial using 2 examples which will help you learn how to control LED brightness using PWM and how to control DC motor speed using PWM.

PWM control using arduino.

PWM control is a very commonly used method for controlling the power across loads. This method is very easy to implement and has high efficiency. PWM signal is essentially a high frequency square wave ( typically greater than 1KHz). The duty cycle of this square wave is varied in order to vary the power supplied to the load. Duty cycle is usually stated in percentage and it can be expressed using the equation : % Duty cycle = (TON/(TON + TOFF)) *100. Where TON is the time for which the square wave is high and TOFF is the time for which the square wave is low.When duty cycle is increased the power dropped across the load increases and when duty cycle is reduced, power across the load decreases. The block diagram of a typical PWM power controller scheme is shown below.

pwm controller block diagramControl signal is what we give to the PWM controller as the input. It might be an analog or digital signal according to the design of the PWM controller. The control signal contains information on how much power has to be applied to the load. The PWM controller accepts the control signal and adjusts the duty cycle of the PWM signal according to the requirements. PWM waves with various duty cycle are shown in the figure below.

In the above wave forms you can see that the frequency is same but ON time and OFF time are different.Two applications of PWM control using arduino is shown here. Controlling the LED brightness using arduino and motor speed control using arduino.

LED brightness control using arduino.

This one could be the simplest example of PWM control using arduino. Here the brightness of an LED can be controlled using a potentiometer. The circuit diagram is shown below.

lIn the circuit, the slider of the 50K potentiometer is connected to analog input pin A0 of the arduino. The LED is connected at digital pin 12 of the arduino. R1 is a current limiting resistor. The working of the program is very simple. Arduino reads the voltage at the analog input pin A0 (slider of the POT). Necessary calculations are done using this reading and the duty cycle is adjusted according to it. The step-by-step working is noted in the program below.

Program.

int pwm = 12; // assigns pin 12 to variable pwmint pot = A0; // assigns analog input A0 to variable potint t1 = 0; // declares variable t1int t2 = 0; // declares variable t2void setup() // setup loop{pinMode(pwm, OUTPUT); // declares pin 12 as outputpinMode(pot, INPUT); // declares pin A0 as input}void loop(){t2= analogRead(pot); // reads the voltage at A0 and saves in t2t1= 1000-t2; // subtracts t2 from 1000 ans saves the result in t1digitalWrite(pwm, HIGH); // sets pin 12 HIGHdelayMicroseconds(t1); // waits for t1 uS (high time)digitalWrite(pwm, LOW); // sets pin 12 LOWdelayMicroseconds(t2); // waits for t2 uS (low time)}

Example.

The following example helps you to understand the stuff better.

Suppose the slider of the potentiometer is adjusted so that the voltage at its slider is 3V. Since the slider terminal is connected to A0 pin, the voltage at A0 pin will be also 3V. analogRead function in arduino reads the voltage (between 0 to 5V) at the analog input pin,converts it in to a digital value between 0 and 1023 and stores it in a variable.

Since the analog input voltage here is 3 volts the digital reading will be 3/(5/1023) which is equal to 613. This 613 will be saved to variable t2 (low time). Then t2 is subtracted from 1000 and the result which is 387 is stored in variable t1 (high time). Then digital pin will be switched on for t1 uS and switched off for t2 uS and the cycle is repeated. The result will be a square wave with high time = 387 uS and low time = 613 uS and the time period will be always 1000uS. The duty cycle of this wave form will be (387/(387+613))*100 which is equal to 38.7%. The wave form will look something like what is shown below.

Motor speed control using arduino.

Circuit diagram of DC motor speed control using arduino is shown in the figure below. The working principle and program of this circuit is same as that of the LED brightness control. Only difference is that and additional motor driver circuit using a transistor is included in the circuit. Each digital pin of the arduino can sink or source only 40mA. DC motors usually consume much more than this and it is not safe to directly connect a heavy load to the digital pin.

dc motor speed control using arduinoIn the circuit diagram, slider of the potentiometer is connected to analog input pin A0 of arduino. Resistor R1 limits the base current of the transistor Q1. Motor is connected as collector load to the transistor. Capacitor C1 by-passes voltage spikes and noises produced by the motor. This filter capacitor is very essential and if it is not there the circuit may not work properly.Program.

int pwm = 12; // assigns pin 12 to variable pwmint pot = A0; // assigns analog input A0 to variable potint t1 = 0; // declares variable t1int t2 = 0; // declares variable t2void setup() // setup loop{pinMode(pwm, OUTPUT); // declares pin 12 as outputpinMode(pot, INPUT); // declares pin A0 as input}void loop(){t2= analogRead(pot); // reads the voltage at A0 and saves in t2t1= 1000-t2; // subtracts t2 from 1000 ans saves the result in t1digitalWrite(pwm, HIGH); // sets pin 12 HIGHdelayMicroseconds(t1); // waits for t1 uS (high time)digitalWrite(pwm, LOW); // sets pin 12 LOWdelayMicroseconds(t2); // waits for t2 uS (low time)}

Notes.

In both circuits shown above the arduino is supposed to be powered through the 9V external power input jack.+5V supply for the potentiometer can be taken from the 5V regulator output on the arduino board.The DC motor I used while testing was rated 9V/100mA.The LED I used while testing was a general purpose 4mm bright green LED.The maximum collector current 2N2222 can handle is 800mA. Keep this in mind while selecting the motor.Be very careful while handling the arduino board. Any wrong connections might damage the boardArduino frequency counter

We use the Arduino Uno as the main controller. In principle Arduino frequency counter is very simple, which is the input frequency to be measured is inserted into the port counters Arduino Uno. In Arduino uno counter 1 ( T1 ) is at a digital port 5. To view our Arduino frequency counter using a 204 LCD. Here is a arduino frequency counter that we made:

In this series of Arduino frequency counter ICs 74LS14 we use as a stabilizer pulses before entering the port arduino uno. Here is a schematic drawing:arduino frequency counter schematic circuit 500x453 Arduino frequency counter, guide how to make it?

Pulse frequency to be measured in the insert IC 74LS14 to pin 13 and the output on pin 12 then insert it into the port counters 1 on Arduino Uno port Digital 5, then pulse in the process. 204 LCD Display pin 4 ( RS ) connected to a digital port 12, to pin 6 ( E ) is connected to the digital port 11. for port 5 ( RW ) must be connected to ground, and to port the LCD data pin 11, 12, 13, 14 are connected to Arduino digital port 9, 8, 7, 6.

Arduino frequency counter uses two libraries, LiquidCrystal.h for LCD and FreqCounter.h for library frequency counter. frequency counter for the library can be downloaded library Arduino frequency counter Here is the source code for reading the pulse frequency:

FreqCounter::start(1000); // set periodewhile (FreqCounter::f_ready == 0) // wait until the counter finishes

freq=FreqCounter::f_freq; //read the results

lcd.clear();lcd.setCursor(1,0);lcd.print("Frequency Counter");lcd.setCursor(5,1);lcd.print("====**====");lcd.setCursor(4,2);lcd.print(freq);lcd.setCursor(15,2);lcd.print("Hz");delay(20);

in this experiment we use to signal frequency waveform generator circuit with Arduino, maybe next time we will be posting. Keep in mind the signal pulse amplitude of 5V.

Here is the program Arduino frequency counter code full:

/* ===============================================================Arduino frequency counter

This program is a free codingDesign By : http://circuitdiagram-schematic.com/arduino-frequency-counter/Components: LCD 20x4 on I/O ports 12, 11, 9, 8, 7, 6 Counter 1 on ports 5===============================================================*/

#include #include

LiquidCrystal lcd(12,11,9,8,7,6);unsigned long freq;

void setup() {lcd.begin(20, 4); lcd.setCursor(1,0);lcd.print("Frequency Counter");lcd.setCursor(7,1);lcd.print("--**--");lcd.setCursor(4,2);lcd.print("Designed By:");mySite();

}

void loop() { FreqCounter::start(1000); // set periodewhile (FreqCounter::f_ready == 0) // wait until the counter finishes

freq=FreqCounter::f_freq; //read the results

lcd.clear();lcd.setCursor(1,0);lcd.print("Frequency Counter");lcd.setCursor(5,1);lcd.print("====**====");lcd.setCursor(4,2);lcd.print(freq);lcd.setCursor(15,2);lcd.print("Hz");delay(20);}

void mySite(){lcd.setCursor(0,3);lcd.print("Circuitdiagram-schem");delay(1500);

lcd.setCursor(0,3);lcd.print("ircuitdiagram-schema");delay(1500);lcd.setCursor(0,3);lcd.print("rcuitdiagram-schemat");delay(1500);lcd.setCursor(0,3);lcd.print("cuitdiagram-schemati");delay(1500);lcd.setCursor(0,3);lcd.print("uitdiagram-schematic");delay(1500);lcd.setCursor(0,3);lcd.print("itdiagram-schematic.");delay(1500);lcd.setCursor(0,3);lcd.print("tdiagram-schematic.c");delay(1500);lcd.setCursor(0,3);lcd.print("diagram-schematic.co");delay(1500);lcd.setCursor(0,3);lcd.print("iagram-schematic.com");delay(1500);}

For circuit board Arduino frequency counter can be downloaded below, to use the password: circuitdiagram-schematic.com

Digital Thermometer with Arduino Uno

In this article we make digital thermometer on the Arduino Uno. In this Digital Thermometer temperature sensor used is LM35DZ. In other applications you can replace with LM35 temperature sensor such as DS18B20 and others. In principle, the sensor will detect the temperature, then go to the ADC (analog to digital converter) on the arduino Uno and processed, and then displayed on the LCD. In this digital thermometer LCD used is Hitachi HD44780 chip. Liquid crystal displays (LCDs) offer a convenient and inexpensive way to provide a user interface for a project. Here is a picture digital thermometer:

In this circuit the output port LM35DZ temperature sensor, connected to the ADC analog port A0 on arduino uno. LCD display digital thermometer, digital port pin of the LCD to the Arduino Uno is:

Shematic LCD to Arduino Uno Circuit Digital Thermometer with Arduino Uno

Source Code Digital Thermometer Digital Thermometer with Arduino Uno

/*
An open-source LM35DZ Temperature Sensor for Arduino. This project will be enhanced on a regular basis (Digial Thermometer) by http://circuitdiagram-schematic.com/
*/

// library LCD
#include

// Initialize the degree symbol
const byte degreeSymbol = B11011111;

// Initialize the library with columns and rows
const int numRows = 2;
const int numCols = 16;

// Initialize the library with numbers of interface
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// analog pin
const int pin = 0;

// temperature variables
int therC = 0,therF= 0;

// variables to make a better precision
int samples[8];

// to start max/min temperature
int maxi = -100,mini = 100;
int i;

void setup()
{
// LCD initialization
lcd.begin(numCols, numRows);
lcd.print(" Temperature");
lcd.setCursor(4, 1);
lcd.print(degreeSymbol);
lcd.setCursor(5, 1);
lcd.print("C");
lcd.setCursor(8, 1);
lcd.print("/");
lcd.setCursor(13, 1);
lcd.print(degreeSymbol);
lcd.setCursor(14, 1);
lcd.print("F");
}

void loop()
{
// gets 8 samples of temperature
for(i = 0; i maxi) {maxi = therC;}

// set min temperature
if(therC < mini) {mini = therC;}

// Celsius on LCD displays
lcd.setCursor(1, 1);
lcd.print(therC,DEC);

// fahrenheit on LCD displays
lcd.setCursor(10, 1);
lcd.print(therF,DEC);

therC = 0;

delay(1000); // delay before loop
}
In this piece of coding digital thermometer you can do to change the calibration value. In this experiment we include the value of 8.6. You can change the value to 9 or 10. You will find the difference. Our advice to perform the calibration, after changing the settings on coding the value of digital thermometer, do the comparison with a digital thermometer on the market. You can download a digital thermometer coding below.

DIY Digital Clock DS1307 RTC with Arduino Uno and LCD

Do you want to make your own digital clock. Here is schematic digital clock using arduino Uno with the display on the LCD (liquid crystal displays). To make this you need a digital clock circuit RTC (Real Time Clock). You can create your own series of RTC circuit board with ease, schematic digital clock using DS1307 IC and several other components such as capacitors, resistors, battery 3V and crystals, especially for the size of the crystal used is 32.768MHz, and connect the body to the ground to avoid the crystal interference from outside. Here is schematic RTC DS1307 and DS1307 RTC PCB:

Schematic DS1307 RTC DIY Digital Clock DS1307 RTC with Arduino Uno and LCD

PCB DS1307 RTC DIY Digital Clock DS1307 RTC with Arduino Uno and LCD

DS1307 RTC DIY Digital Clock DS1307 RTC with Arduino Uno and LCD

Besides RTC DS1307 series, you also need an Arduino Uno and the LCD, Heres the schematic of the cable connecting the DS1307 RTC Arduino Uno and LCD:

cable connecting the DS1307 RTC Arduino Uno and LCD1 DIY Digital Clock DS1307 RTC with Arduino Uno and LCD

DIY Digital Clock DS1307 RTC with Arduino Uno and LCD DIY Digital Clock DS1307 RTC with Arduino Uno and LCD

The working principle of the digital clock schematic is very simple, all of the data all of the time and date recorded in the IC DS1307. After you create your DS1307 PCB should set up the first time and date, if not before setting the date will start from 1/1/2000 and at 0:0:0. For that you should set up the first way by using coding like this, you just remove the words // in the sketch on the arduino, as shown below:

// following line sets the RTC to the date & time this sketch was compiled //RTC.adjust(DateTime(__DATE__, __TIME__));

become to

// following line sets the RTC to the date & time this sketch was compiled RTC.adjust(DateTime(__DATE__, __TIME__));

After you remove the words // you click upload and wait for it to finish, it will appear on the LCD writing the date and hours of time are now following the date shown on your PC or laptop you use. If you return the completed writing // and upload back to avoid changing the date and time, it is very powerful when you connect the power supply by USB cable to a PC or laptop arduino different. In this digital clock schematic relationship with arduino Uno DS1307 RTC using I2C connection system. Warning do not remove battery 3V, if released it will return to the time clock and date 1/1/2000 0:0:0. Here is a snippet of code digital clock DS1307 RTC:

snippet of code digital clock DS1307 RTC DIY Digital Clock DS1307 RTC with Arduino Uno and LCD

In this article weve included a digital clock program code and PCB circuit board DS1307 RTC, you can download below, to use the password: circuitdiagram-schematic.com