Chapter 3
Programming With Arduino
This chapter starts much theory, we begin directly with programming. "Learning by doing" is the magic word here. While the left side of the so-called "sketches" are printed and are located in the right pane Reviewing the code.
Anyone who works through the programs under this system will be able to see through the program code in a short time and apply yourself. Later even become familiar with other functions you then. This guide is only an introduction to the Arduino platform. All possible program functions or
Program code is on the website "www.arduino.cc " under the point called "reference."
Before we start a piece of brief information on possible errors that might arise while working with the Arduino software. The most common are the following two:
-
The board is not installed properly or selected the wrong board. When Upload, it displays the sketch an error message at the bottom of the software that roughly looks like as shown at right. In error, the text is then a Noted "not in sync."
-
There is an error in the sketch. For example, a word is misspelled, or it lacks only a clamp or a semicolon. In the example, the left brace that introduces the loop part is missing. The error message then begins often with "expected...” This means that the program expects something that does not yet exist.
The Basic Structure of a Sketch
We may divide a sketch into three areas.
1. Variables to Name
In the first area, elements have named the program (What that means, we learn in the program No. 3). This part is not mandatory.
2. Setup (essential in the program)
The setup is performed by the Board only once. Here, dividing the program with, for example, which pin is (slot for cable) on the microcontroller board, an output or an input.
Here, a voltage at the output Board shall: defined as the output. Example: This pin an LED to be illuminated.
Defined as input: Here, a voltage to be read by the Board. Example: a switch is pressed, and the Board noted the fact that he recognizes this input pin voltage.
3. Loop (essential in the program)
The loop portion is repeated continuously by Board. It processes the sketch once completely to the end and then starts again at the beginning of the loop part.
Instructions
A Flashing LED
Task: A light-emitting diode to blink.
Material: Only the microcontroller board with the USB cable.
On the Arduino, an LED is connected to pin 13 is already installed (for test purposes). Often this light blinks on when you connect a new Arduino board, as the flashing program is already pre-installed for testing the board depending on the manufacturer. We will program this flashing now itself.
Circuit
The existing on the board
LED is circled in red in the diagram.
You have to connect to the computer, only the board via an USB cable.
1.1
Program Section 1: variable name
- Here we go, first nothing.
1.2
Program Section 2: Setup
We have only one exit - to pin 13 to a voltage output (the LED will light up eventually.).
We write in the middle of the white box the Arduino software:
void set up()
// Here the setup starts
{
// brace on - Here begins a program section.
}
// curly bracket - Here ends a program section.
In part between the braces, we now introduce the setup information. In this case, "an output pin 13 is supposed to be."
void set up() // Here the setup starts
{
// Here a program section starts.
pinMode(13, OUTPUT); //
Pin 13 is what the output should be.
}
// Here, a program segment ends.
1.3
Program section 3: Loop (main body)
void set up()
// Here the setup starts
{
// Here a program section starts. pinMode(13, OUTPUT); // pin 13 is to be a starting
}
// Here a program segment ends. void loop()
// Here the main program begins
{
// Here a program section starts.
}
// Here a program segment ends.
Now the contents of the loop part, ie, the main program is introduced: THIS IS THE COMPLETE SKETCH:
void set up()
// Here the setup starts
{
// Here a program section starts.
pinMode(13, OUTPUT);
// pin 13 to be an output.
}
// Here a program segment ends. void loop()
// Here the main program begins
{
// Program section begins.
digitalWrite(13, HIGH);
// Turn the the voltage at pin 13 a (LED on). delay(1000); // wait 1000 milliseconds (one second)
digitalWrite(13, LOW);
// Turn the the voltage at pin 13 from (LED off). delay(1000); // wait 1000 milliseconds (one second).
} // Program section
completed.
Finished. The sketch should now look exactly as it appears on the screen is shown on the right. It has to be now uploaded to the board. This works with the red circled button (top left of the software).
1.4
Now we can create variation to the program. For example, the LED will blink rapidly. To do this, we reduce waiting times (from 1000ms to 200ms)
void set up()
// Here the setup starts
{
// Here a program section starts.
pinMode(13, OUTPUT);
// pin 13 to be an output.
}
// Here, a program segment ends. void loop()
// Here the main program begins
{
// Program section begins.
digitalWrite(13, HIGH);
// Turn the the voltage at pin 13 a (LED on). delay(200); // Wait 200 milliseconds
digitalWrite(13, LOW);
// Turn the the voltage at pin 13 from (LED off). delay(200); // Wait 200 milliseconds
} // Program section
completed.
The new Sketch must now again be uploaded to the board. If all goes well, the LED is now flashing faster.
The Change Indicators
Material: Arduino / two light-emitting diodes (blue) / two resistors 100 ohms / Breadboard / cable
Sketch:
void set up()
{
// // We start with the Setup
pinMode(7, OUTPUT);
// pin 7 is an output. pinMode(8th, OUTPUT); // pin 8 is an output.
}
void loop()
{
// The main program starts.
digitalWrite(7, HIGH);
to Pin7 // Turn on the LED. delay(1000); // wait 1000 milliseconds. digitalWrite(7, LOW); // Turn the LED off at Pin7. digitalWrite(8th, HIGH); // Turn the LED on a Pin8. delay(1000); // wait 1000 milliseconds. digitalWrite(8th, LOW); // Turn the LED off at Pin8.
} // Here at the end of the program jumps to the start of the loop part. So…
//... turn the LED on pin 7.
//... etc... etc... etc...
An LED Pulsing Leave
Task: An LED to be pulsed light and dark. (Also referred to as
English. "Thread")
Material: Arduino / an LED (blue) / A resistor 100 Ohm / Breadboard / cable assembly:
The Arduino is a digital microcontroller. We know it outputs only "5 volts" or "5V off". To vary the brightness of an LED, you would need, but the voltage may vary. For example, 5V when the LED light illuminates. 4 volts when darker lit something so GOING TO DIGITAL PINS BUT NOT. There is an alternative, however. It is called pulse width modulation (PWM called).
The PWM pulsates the 5V voltage. The voltage is thus a millisecond on and off. At a high PWM, the 5V signal is almost continuously on the respective pin. At a low PWM, the 5V signal is virtually non-existent (since this is a very compact summary, you should look on the Internet for further explanation).
This PWM, you can achieve a similar effect with LEDs as if you were the voltage varies. Not all digital pins on the board have the PWM function. The pins on which the PWM works are specially marked, e.g. through a small wave of the number with the pin number. Here we go.
Sketch:
int LED = 9; // The word "LED" is now available for the value. 9
int Brightness = 0;
// The word "brightness" is now available for the value in the
// PWM output. The number 0 is only an arbitrary starting value.
int fade steps = 5;
// fade steps: determines the speed of the "thread" void set up()
{
// Here the setup starts.
pinMode(LED, OUTPUT);
//// The pin with the LED (Pin9) is an output
}
void loop()
{
analog write(LED, brightness);
// The function analog write is here at the
// Pin activated with the LED (Pin9) the PWM output. The PWM value is the value
// stored under the name of "brightness". In this case, "0" (See
// first program section)
brightness + = brightness fade steps; // Now, the value of the PWM output
// changed. Under the value "brightness" is now the previous brightness
// the value for the fade steps added. In this case: brightness = 0 + 5. The
// new value for "brightness" is therefore no longer 0 but 5. Once the loop
// part is once gone through, he repeats. Then the value is for
// the brightness of 10. In the next cycle 15, etc., etc...
delay(25); // The LED should for 25ms (milliseconds), so only very briefly
// maintained brightness. Decreasing this value, the pulsation
// also faster.
if(Brightness == 0 || brightness == 255) { // Command explanation: When the
// brightness has reached the value 0 ORDER 255, the value for the change
// "fade steps" from positive to negative or vice versa. Reason: The LED is
always a bit brighter // first with each pass of the loop part.
// However, at some point the maximum value for the PWM
output with the value 255
//reached. The LED will then gradually become darker. So
// the value for the "fade steps" is at this point Negating (A
// minus sign is placed in front of it)
fade steps = -fadeschritte; // That means for the next pass that in
// the line "brightness = brightness + fade steps," the brightness decreases.
// Example: "brightness = 255 + (- 5)". The value of brightness is from then 250. In
// next pass 245, etc., etc... Once the value for brightness at 0
// has arrived, returns the sign. (Consider the old
// mathematical rule: "minus and minus gives plus").
}
} // This last clip of the loop portion is closed.
Simultaneous Lighting and Sound
Task: An LED and a piezo speaker will continuously flash or beep. Material: Arduino / a LED / A resistor 200 Ohm / A piezo speaker / Breadboard / cable
Sketch:
// This time, we also use the first program section. We enter
// variables. This means that behind a letter or
// one word hides a number. For us, the LED on pin 4 is connected and
// the piezo speaker to pin 5. This is the two pins later not
// confused, we call Pin4 and Pin5 simply.
int LED = 4; // The word "LED" is now available for the number "4".
int pieps = 5;
// The word "beep" is now available for the
number "5". void set up()
{
// We start with the setup.
pinMode(LED, OUTPUT);
// pin 4 (pin "LED") is an output. pinMode(Beeping, OUTPUT); // Pin 5 (Pin "beep") is an output.
}
void loop()
{
// The main program starts. digitalWrite(LED, HIGH); // Turn the light on
digitalWrite(Beeping, HIGH);
// Switch to the piezo speaker. delay(1000); // wait 1000 milliseconds. (It beeps and lights up.)digitalWrite(LED, LOW); // Switch from LED. digitalWrite(Beeping, LOW); // Turn on the piezo out.
delay(1000);
// wait 1000 milliseconds. (No noise, no light)
} // Here at the end of the program jumps to the start of the loop part. so becomes
// it beeps again and lights. If one reduces the pause (delay)
// or enlarged beeps and lights it faster or slower.
Activate a LED Touch of a Button
Task: An LED to be lit for 5 seconds when a button has been operated.
Material: Arduino / an LED (blue) / A resistor 100 Ohm / A resistor 1K ohms (1000 ohms) / Breadboard / cable / probe
The microcontroller can output to its digital pins, not only tensions but also read out. We want to try this program. In the structure, however, there is a special feature. If you simply connect the switch only to the microcontroller, then up to the pin of the microcontroller to a voltage as soon as the button is pressed.
You can think of it that way as if many electrons are floating around the said pin. If we then release the button, no new electrons are more being added to the pin on the microcontroller. Now the rub comes. The electrons that have made it before relaxing on the pin, then still there and escape only very slowly over small leakage currents.
The microcontroller then so thinks that the button is not pressed only briefly, but that it is pressed very long. Namely, until no electrons spend more on the pin, this problem can be correct in that one grounding pin via a resistor to about 1000 ohms (1K ohms). The electrons can thus very quickly drain away from the pin, and the microcontroller detects that the button has been "touched" only briefly.
Since the resistance, the voltage at the input pin to 0V always "pulling down,” it is also referred to as a "pull-down" resistor. NOTE: If you use it too small, resistance can cause a short circuit to the microcontroller when the button is pressed that one grounding pin via a resistor to about 1000 ohms (1K ohms).
The electrons can thus very quickly drain away from the pin, and the microcontroller detects that the button has been "touched" only briefly. Since the resistance, the voltage at the input pin to 0V always "pulling down,” it is also referred to as a "pull-down" resistor. NOTE: If you use it too small, resistance can cause a short circuit to the microcontroller when the button is pressed that one grounding pin via a resistor to about 1000 ohms (1K ohms).
The electrons can thus very quickly drain away from the pin, and the microcontroller detects that the button has been "touched" only briefly. Since the resistance, the voltage at the input pin to 0V always "pulling down,” it is also referred to as a "pull-down" resistor. NOTE: If you use it too small, resistance can cause a short circuit to the microcontroller when the button is pressed.
Sketch:
int LEDblue = 6;
// The word "LEDblue" is now available for the value. 6
int taster = 7;
// The word "taster" is now available for the value. 7
int button status = 0;
// The word "taster status" is now available for the first
// value 0 is later stored under this variable, if the button is pressed
// or not. void set up()
{
// Here the setup starts.
pinMode(LEDblue, OUTPUT);
// The pin with the LED (pin 6) is now an output. pinMode(Push buttons, INPUT); // The pin with the button (pin 7) is now an entrance.
}
void loop()
{
// This clip the loop portion is opened taster status =digital read(Button); // Here the Pin7 is read
// (Command: digital read). The result is below the variable "taster status" with
// the value "HIGH" for 5V or "LOW" saved for 0Volt.
if (Taster status == HIGH)
// Processing: When the pushbutton is pressed (The
// voltage signal is high)..
{
Open // Program section of the IF command. digitalWrite(LEDblue, HIGH); //..dann should light up the LED..
delay(5000);
Although for //..und for 5 seconds (5000 milliseconds). digitalWrite(LEDblue, LOW); // then the
LED off should be.
}
// close the program section of the IF command. else //... otherwise...
{
Open // Program section of the else command digitalWrite(LEDblue, LOW); //... to be the LED off.
}
// close the program section of the else command
} // This last clip of the loop part
closed.
An RGB LED Drive
Task: An RGB LED should light up in different colors
Material: Arduino board / RGB-LED / three resistors with 200 Ohm / Breadboard / Cable
What is an RGB LED?
An RGB LED is an LED that can light up in different colors. After the name of the RGB Color Bet "Red,” "Green" and hide
"Blue." The LED is in the interior of three individually addressable LEDs, which shine in the three colors. Therefore, an RGB LED also has so many legs, for example, 4/6/8 legs . The longest of the four legs depending on the version of the common anode (+) or
Cathode (-). With the three shorter legs of the RGB LED, we can control each color.
Version a "common cathode" - the longest leg of the LED is "-,," and the three shorter legs are controlled by "+" (voltage).
Version b) "common anode" - the longest leg of the LED is "+," and the three shorter legs are - controlled (GND). ""
Through a blend of colors, many other colors can be produced very. For example, results from the control of the color "blue" and "green" color
"Turquoise."
Which version you own, you can find by simply repositioning of "+" and "-" on the LED out (Info: An LED lights up only when properly mated)
The Arduino is a digital microcontroller. He knows his digital outputs only "5 volts" or "5V off". So you can generate many different colors with an RGB LED each color of the LED but must be controlled more accurately. This can be done with pulse width modulation and the PWM (pulse width modulation) can be used to the digital pins on the board at which it prints a small shaft.
The PWM allows the voltage between + 5V and 0V pulsate. The voltage is thus a millisecond on and off. At a high PWM, the 5V signal is almost continuously on the respective pin. At a low PWM signal is the 5V virtually non-existent (Since this is a very compact summary, you should look on the Internet for further explanation).
This PWM can achieve a similar effect in LEDs, as would be the voltage varies. The following codes work for both RGB versions alike. It needs only one thing to be observed: In the LED version b (Common anode) should be the value of the "Dark" is set to the 255th. This has the consequence that then, not only a positive voltage at the common positive terminal of the LED but also to the corresponding color. Then, the LED can be no current flowing, and the respective color of the LED remains off between the two contacts.
For this reason, that in this version of the LED, the color is brighter when the value is smaller is also noted for the color mixture. Thus the color lights blue to pin 3 in the sketch bright, if we choose the code for the blue color like this:
int brightness1a = 0;
Sketch 1:
In this code, the three individual colors are sequentially switched on and off.
int LEDblue = 3; // color blue to pin 3 int LEDrot = 5; // color red on pin 5 int LEDgreen = 6; // color green to pin 6
int p = 1000; // p is a break with 1000ms So one second
int brightness1a = 150; // numerical value between 0 and 255 - are
// the luminance of each color at
int brightness1b = 150; // numerical value between 0 and 255 - are
// the luminance of each color at
int brightness1c = 150; // numerical value between 0 and 255 - are
// the luminance of each color at
int dark = 0; // Neumericalvalue 0 means 0V - so LED off void set up()
{
pinMode(LEDblue, OUTPUT); pinMode(LEDgreen, OUTPUT); pinMode(LEDrot, OUTPUT);
}
void loop()
{
analog write(LEDblue, brightness1a); // turn blue delay(P); // Break
analog write(LEDblue, dark); off // blue analog write(LEDrot, brightness1b); // turn on red delay(P); // Break
analog write(LEDrot, dark); rotausschalten //
analog write(LEDgreen, brightness1c); turn green // delay(P); // Break
analog write(LEDgreen, dark); // greenausschalten
}
Sketch 2:
In this code, the three individual colors are in pairs on and off in sequence. Thereby, the color mixing yellow, turquoise, and purple arise.
int LEDblue = 3; // color blue to pin 3 int LEDrot = 5; // color red on pin 5 int LEDgreen = 6; // color green to pin 6
int p = 1000; // p is a break with 1000ms So one second
int brightness1a = 150; // numerical value between 0 and 255 - are
// the luminance of each color at
int brightness1b = 150; // numerical value between 0 and 255 - are
// the luminance of each color at
int brightness1c = 150; // numerical value between 0 and 255 - are
// the luminance of each color at
int dark = 0; // value 0 means 0V - so LED
//out
void set up()
{
pinMode(LEDblue, OUTPUT); pinMode(LEDgreen, OUTPUT); pinMode(LEDrot, OUTPUT);
}
void loop()
{
analog write(LEDgreen, brightness1c); // green and red on yellow = analog write(LEDrot, brightness1b);
delay(P);
analog write(LEDgreen, dark); // green and red from yellow = from analog write(LEDrot, dark);
analog write(LEDgreen, brightness1c); // green, and blue =
//turquoise
analog write(LEDblue, brightness1b); delay(P);
analog write(LEDgreen, dark); // green and blue from turquoise
from = analog write(LEDblue, dark);
analog write(LEDrot, brightness1b); // red and blue = purple analog write(LEDblue, brightness1b);
delay(P);
analog write(LEDrot, dark); // red and blue from purple from = analog write(LEDblue, dark)
}
The Motion
Task: A piezo speaker to beep when motion is detected. Material: Arduino / Motion / Breadboard / cable / piezo speaker
Course content: voltage of a motion to read and use for output.
The motion, also called the PIR sensor, is very simple in construction.
Once it detects motion, it is on a pin a voltage
of 5 volts. This only needs to be read and processed by the microcontroller.
Two knobs can ad the duration of the output signal and sensitivity (range).
The plastic lens is lightly connected. If one sets them apart can be seen the infrared detector and can be seen from the label under the lens, as the sensor has to be wired: GND (-), OUT (output of the signal), VCC (+).
There is also a jumper which allows you to switch between two modes at the bottom of the movement.
-
A jumper is quite outside: it detects The output signal after it maintained a motion for a certain time and then deactivated definitely be back, even if there is a movement that could be detected in the action area of the motion. After a certain time, the output signal is generated again.
-
The jumper is like a right slightly offset on the image inside. The output signal remains constantly active as long as the motion is detected a movement and this mode is recommended for Arduino projects.
Sketch:
int piezo = 5; // The word "piezo" is now available for the value. 5
int motion = 7; // The word "movement" is now available for the value. 7
int motion status = 0; // The word "movement status" first, is now available for the
// value 0 is later stored under this variable, whether a movement
// is recognized or not.
void set up() // Here the setup starts.
{
pinMode(Piezo, OUTPUT); // The pin with the piezo (pin 5) is now an output. pinMode(Move, INPUT); // The pin with the motion detector (pin 7) is now
//an entrance.
}
void loop() // The loop part begins
{ // This brace is the Loop section opened. motion status =digital read(Move); // ier of Pin7 is read. The
// result is under the variable "moving status" with the value "HIGH" for
// 5V or "LOW" saved for 0Volt.
if (Movement status == HIGH) // processing: When motion is detected
// (The voltage signal is high)
{ Open // Program section of the IF command. digitalWrite(Piezo, HIGH); // then the piezo will beep. delay(5000); Although //...and for 5 seconds. digitalWrite(Piezo, LOW); //...then it should be quiet.
} // close the program section of the IF command. else //otherwise...
{ Open // Program section of the else command.
digitalWrite(Piezo, LOW); //...should the piezo speaker to be.
} // close the program section of the else command.
} // This last clip of the loop portion is closed.
Read Brightness Sensor
Now it's getting more complicated.
Task: An LED should light when it gets dark or when a photoresist is covered.
Material: Arduino / a LED / resistor with a 200 Ohm / a resistor of 10K ohms / Breadboard / cable / photoresistor
Read Voltages
The microcontroller is to read about a photoresistor how bright it is. For this, one uses a simple physical principle. If in a circuit two consumers are connected one behind the other (in series), then "shared" it is also commonly applied voltage. An example: two identical lamps connected in series, and there is applied a voltage of 6 volts.
Then you can determine with a voltmeter that each abut the lamps only 3 volts. If we connect two unequal lamps (One has a lower
resistance), then one can use two different voltages to the two lamps measure, for example. 1.5 volts and 4.5 volts. A photoresistor changes its resistance as a function of light intensity. This effect is exploited so one can generate a voltage division here at all, and it closes the photoresistor and a resistor (1 - 10 K ohms, depending on the photoresistor. The resistor should have a similar resistance value as that of the photoresistor have.) in series and connects it to 5 volts and the "grounding" (ground / GND) - see construction. The microcontroller board is to measure capability of analog signals (voltage) and to process these.
We can do this with the analog inputs on the board and this converts the measured voltage value into a number that can then be further processed. 0 volt corresponds to the number 0, and the highest measured value 5 volts corresponds to the number 1023 (0 to 1023 equals 1024 digits = 10 bits). Example: A voltage of 2.5 volts is measured, then the microcontroller returns the value 512 (1024: 2).
The "Serial Monitor"
The "serial monitor" is an important part of the Arduino software. This "serial monitor" you can display data on the PC, which the microcontroller board to the PC sends (numbers or text). This is very useful because you do not an LCD display that is always connected to the microcontroller, on which one could read certain values.
In the sketch, the "serial monitor” is used to display the values that read the board of the photoresistor. Why is this useful? Suppose the LED to begin only at the onset of darkness to light. Then it must be in an area Sketch gives, which has the function: "If the value of the photo resistance below the value x, then the LED lights will." It would have to know how big the value of x at the onset of twilight.
Solution: (. Eg dusk) I send the read value "x" of the voltage across the photoresistor with appropriate brightness at the "serial monitor" and let him see me there. With this knowledge, I can later modify the
program in the following form. "If the voltage output of the photoresistor has a value of" x "below, then turn on the LED."
Sketch:
int input = A0;
// The word "input" is now available for the value "A0" (name
// the analog port 0)
int LED = 10;
// The word "LED" is now available for the value 10
int Sensor value = 0;
// variable for the sensor value with 0 as the start value
void set up()
// Here the setup starts.
{
Serial.begin(9600);
// Communication with the serial port
// started. This one needs to be the actual value read later
let // Display serial monitor in.
pinMode (LED,OUTPUT);
// The pin with the LED (pin 10) is now an output
}
void loop()
{
// This brace is the Loop section opened. Sensor value =analog read(entrance); // Read the voltage on the photoresistor
// store and under the variable "sensor value". Serial.print("Sensor value =");// Output on Serial Monitor: The word "sensor value"
Serial.print(Sensor value);
// Output on Serial Monitor. The command
the sensor value of the photoresistor is in the form of a number //Serial.print
// sent 0-1023 to the serial monitor.
if(Sensor value> 512)
// If the sensor value is over 512....
{
digitalWrite(LED,HIGH);
//... is to light the LED...
}
else
{
digitalWrite(LED,LOW);
//... otherwise, they will not shine
}
delay(50);
// A short break, during which the LED is on or off
}
// This last clip of the loop portion is closed.
// Now, if the sensor value, for example, at normal brightness. Only the value of 100 has
// (The value depends on the used resistors of the brightness
// and of the current direction), then take the place of the value 512
// much smaller value at which the LED will begin to glow. For example.
// then you take the value of 90. The current sensor value you can now use
// using the "Serial monitor" display. Click at the top "Tools."
// and then click "serial monitor."
Knobs
Task: An LED should blink. The blink rate is to be added with a knob.
Material: Arduino / a knob (potentiometer) / Breadboard/cable read voltage of a rotary control, processing sensor values, and mathematically to use for output (in this case, for the duration of a pause): learning content.
A knob has three terminals. Outside is + and - connected. From the middle, pin cable goes to an analog input pin on the microcontroller board. By turning the knob, the middle pin outputs a voltage between 0 and 5 volts. Knobs far left: 0 V and knobs on the far right: 5V, or mirror-inverted depending on the wiring.
As LED should blink, we use as the first sketch the LED, which is secured by pin 13 on the microcontroller. In addition, it still connects further LEDs.
Sketch:
int input = A0;
// The word "input" is now available for the value "A0" (name
// the analog port 0)
int LED = 13;
// The word "LED" is now available for the value 13
int Sensor value = 0;
// variable for the sensor value with 0 as the start value void set up()
{
// Here the setup starts.
pinMode (LED,OUTPUT); // The pin with the LED (pin 13) is now an output.
}
void loop()
{
// This brace is the Loop section opened.
Sensor value = analog read(entrance);
// The voltage on the control dial will read
// and in the previous sketch as a number from 0 to 1023 under the variable
// "sensor value" stored.
digitalWrite (LED,HIGH);
// The LED is turned on
delay(Sensor value);
// The LED remains on for so many milliseconds,
// as the value of "sensor value" it has stored. digitalWrite(LED, LOW); // The LED is turned off.
delay(Sensor value);
// The LED remains off for so many milliseconds,
// as the value of "sensor value" it has stored.
}
// This clip the loop portion is closed
// The loop part is now restarted. If the value of the read
// knob changes, then the time between the input and output changes
// phases of the LED. The flashing is faster and slower. The longest
In this skit // delay is 1023ms (milliseconds). If you long delays
// required then builds to a small mathematical line in the code.
// For example, to change the line "sensor value = analog read (input)," in
// "sensor value = analog read (input) * 2;" This is the stored sensor value
// magnified by a factor 2nd Since longest delay would be 2046ms etc.
Measure Temperatures
Task: With the temperature sensor TMP36, the temperature should read and display monitor serial-by.
Material: Arduino / Breadboard / cable / Temperature Sensor TMP36 / External Power Supply
The sensor has three connections. When looking at the flat side of the sensor: left 5V, GND, and right in the middle of the pin for the temperature signal. On this pin, the sensor outputs a voltage between 0 and 2.0 volts. Wherein 0V -50 ° C and the corresponding value of 2.0V corresponds to 150 ° C.
According to the manufacturer, the sensor is between -40 ° C and + 125 ° C with reasonable accuracy (± 2 ° C). The voltage of this pin has to read from the microcontroller board and converted into a temperature value.
NOTE:
-
If the sensor is connected incorrectly, it will burn out.
-
In the structure of an external power supply should be used if possible, as it improves considerably the sensor accuracy (9V AC adapter or 9V battery).
Sketch:
int TMP36 = A0;
// The sensor A0 is to be connected to the analog pin. We
// call the pin from now "TMP36"
int temperature = 0; // Under the variable "temperature" is later
// temperature value stored
int temp [10];
// This yields good values, you first read multiple values
// and then averages the results. The square bracket "[10]" generated here
// equal to ten variables named "temp [0],” "temp [2],” "temp
[3]"... to...
//"temp[9]".Mit the notation [10] So you can save only a little Place.
int time= 20;
// The value for "time" is in the code, the time intervals
// ago between the individual measurements. void set up() {
Serial.begin(9600);
// In the setup we start serial communication so
// we can show us the temperature later. About the serial
// communication the board, sending measurements to the computer. In the Arduino
// software can be under "Tools" the "Serial Monitor" start the readings
// to see.
}
void loop() {
temp [0] = map(analog read(TMP36),
0
410
-50, 150);
delay(time);
temp [1] = map(analog read(TMP36),
0
410
-50, 150);
delay(time);
temp [2] = map(analog read(TMP36),
0
410
-50, 150);
delay(time);
temp [3] = map(analog read(TMP36),
0
410
-50, 150);
delay(time);
temp [4] = map(analog read(TMP36),
0
410
-50, 150);
delay(time);
temp [5] = map(analog read(TMP36),
0
410
-
50, 150);
delay(time);
temp [6] = map(analog read(TMP36),
0
410
-50, 150);
delay(time);
temp [7] = map(analog read(TMP36),
0
410
-50, 150);
delay(time);
temp [8] = map(analog read(TMP36), 0, 410, -50, 150); delay(time);
temp [9] = map(analog read(TMP36), 0, 410, -50, 150); // From here is ten times the
read // temperature. In between is ever a break with the duration
// "time" in milliseconds. But what happens here, exactly? Let's look at the
// command once more closely.
// temp [1] = map (analog read (TMP36), 0, 410, -50, 150);
//Sequentially:
// temp [1] - is the name of the first variable.
// "map (a, b, c, d, e)" - This is the so-called "Map command". Allows
// to a read-in value (a) in which the values between (b) and (c) lie,
// convert it into a different speed range, and that (d) in the area between
// and (e).
// following our command happens:
// the value of the sensor is read out directly in the "Map" command with
// "analog read (TMP36)". The measured values should be between 0 and 410th The
// corresponds to the analog port of the values between 0V and 2V. is the voltage
// the sensor out at temperatures between -50 ° C and + 150 ° C. These values on
// analog port will now be through the "Map command" directly to the Celsius values
// -50 to 150 converted.
temperature = (temp [0] + temp [1] + temp [2] + temp [3] + temp [4] + temp [5] + temp [6] + temp [7] + temp temp [8] + [ 9]) / 10; // All in a row. Here every ten are found
// temperature values added together and divided by ten. The
// average value is stored under the variable "temperature"
Serial.print(temperature);
// Now, the value of "temperature" over the serial
// sent communications to the PC. Now you have the serial monitor in the
// Open Arduino software to the temperature at the sensor to read. Serial.println("Degree");
}
Expansion of the Program:
Once the temperature has reached 30 ° C, to a warning sound (beep of a piezo speaker).
int TMP36 = A0;
int temperature = 0; int temp [10];
int time= 20;
int piezo = 5;
// The word "piezo" is now the number 5, that is to port 5 of the
// Piezo connected. void set up() { Serial.begin(9600);
pinMode (Piezo, OUTPUT);
// The piezo speaker to pin 5 should be a starting
// (logical because of the yes from the microcontroller board yes a voltage required to
// beeping.
}
void loop() {
temp [0] = map(analog
0
410
-50,
150);
read(TMP36),
delay(time);
0
410
-50,
150);
temp [1] = map(analog read(TMP36),
temp [9] = map(analog
0
410
-50,
150);
read(TMP36),
Measure Distance
Task: the ultrasonic sensor HC-SR04 and a microcontroller Arduino a distance to be measured and displayed with the "serial-monitor".
How does the ultrasonic sensor? The sensor has four terminals.
a) 5V (+) b) GND (-) c) echo d) trigger
The connections 5V and GND are self-evident; they supply the sensor with power.
The "trigger" of the contacts inserts a short signal (5 V) into the
microcontroller board, causing an acoustic wave through the ultrasonic sensor. Once the sound wave hits a wall or other object, it is reflected and at some point reaches the ultrasonic transducer again.
Once the transducer detects these reflected sound waves, it sends a 5V signal to the "echo" contact on the microcontroller board. The microcontroller only measures the time between the radiation and the return of the sound wave and converts this time into distance. Here we go.
Material: microcontroller board / cable / Breadboard / Hc-SR04 ultrasonic sensor
Code:
int trigger = 7;
trigger // The word is now the number. 7 int echo = 6; // The word echo is now the number sixth
long time = 0;
life // The word is now a variable under which the time
// is stored, to the reflection and requires a sound wave back.
// Start value here 0th
long distance = 0;
// The word "distance" is now variable, under the
is stored // the calculated distance.
// Info: Instead of "int" is faced with the two variables "long." That has
// the advantage that a larger number can be saved. Disadvantage:
// variable takes up more space in memory. void set up()
{
Serial.begin (9600);
// start communication Serial, so you later
can view the serial monitor // values.
pinMode(Trigger, OUTPUT);
// "trigger" (pin 7) is an output. pinMode(echo, INPUT); // "echo" (pin 6) is an input.
}
void loop()
{
digitalWrite(Trigger, LOW);
// Here, take the voltage for a short period
// trigger pin, so that one later sending the trigger signal a noise-free
// Signal.
delay(5);
// Duration: 5 milliseconds
digitalWrite(Trigger, HIGH);
// Now you send off an ultrasonic wave delay(10); // This "tone" is heard for 10 milliseconds. digitalWrite(Trigger, LOW); // Then the "tone" is turned off.
duration = pulseIn(echo, HIGH);
// The command "pulseIn" is one of the
// microcontroller the time in milliseconds until the sound to
// ultrasonic sensor returns.
Distance = (time / 2) / 29.1;
// Now we calculate the distance in
// centimeters. First, you divide the time by two (because only one
// Section wants to calculate and not back the track and back). The value
// can still be divided by 1.29 and then receives the value in centimeters because of the
// sound in the air at a speed of one centimeter per 29.1
// microseconds.
// Example: The sensor measures time from 2000 microseconds. These are then
// 1000 microseconds each way. Now one in 1000 because of the sound divided by 1.29 (
// yes 1cm travels per 29.1 microseconds) and receives the value 34,3cm
if (Distance> = 500 || distance <= 0)
// If the measured distance over
// is 500cm or 0cm...
{
Serial.println("No reading");
// then is to spend the serial monitor "No
// reading "because readings are wrong or inaccurate in these areas.
}
else
//otherwise..
{
Serial.print(distance);
//... should the value of the distance to the serial monitor
// output here.
Serial.println("cm");
}
delay(1000);
// The delay of one second provides in about every new second for
// a new measurement.
}
Expansion of the Program:
When a distance is measured 80cm, a piezo speaker to beep.
int trigger = 12; int echo = 13; long time = 0;
long distance = 0;
int piezo = 5;
// The word piezo is now the number 5 void set up()
{
Serial.begin (9600); pinMode(Trigger, OUTPUT); pinMode(echo, INPUT);
pinMode(Piezo, OUTPUT);
// The piezo speaker to pin 5 should be a starting
// (logical because the yes from the microcontroller board yes a voltage required to
beeping //
}
void loop()
{
digitalWrite(Trigger, LOW);
delay(5); digitalWrite(Trigger, HIGH); delay(10); digitalWrite(Trigger, LOW); duration =pulseIn(echo, HIGH); Distance = (time / 2) / 29.1;
if (Distance> = 500 || distance <= 0)
{
Serial.println("No reading");
}
else
{
Serial.print(distance); Serial.println("cm");
}
if (Distance <= 80)
// If the value for the distance below or equal to 80
//, then...
{
digitalWrite(Piezo,HIGH);
//... start beeping.
}
else
// And if that is not so...
{
digitalWrite(Piezo,LOW);
//... then be quiet.
}
delay(1000);
}
Enhancements: Reversing Warning
With this code, we can construct a reverse warning system, on pin 12 besides "measure distance" to the already connected ultrasonic sensor from the Sketch 10 (connects an LED.
... you can wrap an image already follows construct the reverse warning anyway?
int trigger = 7; int echo = 6; long time = 0; int LED = 12;
long distance = 0;
void set up()
{
Serial.begin (9600); pinMode(Trigger, OUTPUT); pinMode(echo, INPUT); pinMode(12, OUTPUT);
}
void loop()
{
digitalWrite(Trigger, LOW); delay(5); digitalWrite(Trigger, HIGH); delay(10); digitalWrite(Trigger, LOW); duration =pulseIn(echo, HIGH); Distance = (time / 2) / 29.1;
if (Distance> = 500 || distance <= 0)
{
Serial.println("No reading");
}
else
{
Serial.print(distance); Serial.println("Cm");
}
if (Distance <= 40)
{
digitalWrite(LED, HIGH); delay(Distance * 3); digitalWrite(LED, LOW); delay(Distance * 3);
}
}
Infrared Remote Control for Controlling Arduino Microcontrollers
Material: Arduino / Breadboard / cable / infrared sensor / infrared remote control
With an infrared receiver that an Arduino board evaluates the commands of an infrared remote control. The data is sent in the form of infrared light from the remote control to the receiver. Since our eyes can not perceive this light, we can’t see it.
With a little trick, however, you can test whether a remote control, for example, sends an infrared signal. This involves taking a digital camera (for example, by phone) and viewed via the display, the infrared diode. Now, if we press the remote control, you can see the lights infrared diode because the sensors of digital cameras perceive infrared light and can represent. The light flickers easily, since the infrared diode toggle very quickly and outgoing. Underlying this is a very specific rhythm that can test the basis of which the infrared receiver later, which key was pressed on the remote control.
This sketch is a slight modification of Sketches "IRrecvDemo," default in the Arduino main program can not find which under the examples.
This sketch is very short and therefore lends itself very well for the first experiments.
Code:
/ *
*
IrRemote: IRrecvDemo - Demonstrates receiving IR codes with IRrecv
*
An IR detector/demodulator must be connected to the input RECV_PIN.
*
Version 0.1 July 2009
*
Copyright 2009 Ken Shirriff
*
http://arcfn.com
* /// Information over the original program "IrrecvDemo". #include <Irremote.h> // The program encroaches on a
// "Library" back. This does a lot of work. Because of the infrared light
// will be encrypted with a code. To read this code itself
// convert and appropriate values, a lot of lines of code would be required.
int RECV_PIN = 11;
// outputs The contact of the infrared sensor data,
// is connected to pin 11 of the Arduinoboards.
IRrecv irrecv (RECV_PIN);
// At this point, an object is defined that
// reads the infrared sensor to pin. 11
decode_results results;
// This command ensures that the data by
// infrared is read stored under "results."
void set up()
{
Serial.begin(9600);
// In the setup the serial connection is started, so
// you look at the data to receive the remote control via the serial monitor
// can.
pinMode (13, OUTPUT);
irrecv.enableIRIn();
// This command initializes the infrared receiver.
}
void loop()
{
// The loop portion turns out very shortly by resorting to
the "library". if (irrecv.decode(Results)) { // If data has been received, Serial.println (results.value, DEC); // they are as a decimal (DEC) to the
// Serial Monitor output.
irrecv.resume();
// The next value to be read from the IR receiver
}
One press on the button "1" of the infrared remote control unit causes the number "16724175" is output at the Serial Monitor. This is the decoded numerical code behind this button.
If you hold the button down permanently, the number "4294967295" is displayed permanently. This indicates the code that a key is pressed permanently. This number is regardless of which button is pressed, but it can also appear other numbers if a button is pushed only very short or pulsed. In this case, the sensor can read no unique value.
Expansion of the Program:
When pressing the "1" LED will go on and on pressing the button "2,” the LED will go out.
#include <Irremote.h> int RECV_PIN = 11;
IRrecv irrecv (RECV_PIN);
decode_results results; void set up()
{
Serial.begin(9600);
pinMode (13, OUTPUT);
// To pin 13 an LED is connected. digitalWrite(13, LOW); // This should first of all be irrecv.enableIRIn();
}
void loop() {
if (irrecv.decode(Results)) { Serial.println(Results.value, DEC);
if (Results.value == 16724175)
// If the infrared receiver, the number
// read "16724175" (the key According to "1" on the remote control) has
{digitalWrite (13, HIGH)}
// the LED should go on.
if (Results.value == 16718055)
// If the infrared receiver, the number
// read has "16718055" (corresponding to "2" of the remote control button)
{digitalWrite (13, LOW)}
// the LED should go out. irrecv.resume();
}
}
Servo Drive
Task: A servo is to be controlled by an Arduino microcontroller. The servo should drive to three different positions in this example and wait a short time between positions.
Material: A microcontroller board, a servo, three plug-in cable
Code:
#include <Power.h>
// The servo library is called.
Power servo-blue;
// Create the program a servo with the name
// "servo blue"
void set up()
{
servo blue.attach(8th);
// servo is connected to Pin8
}
void loop()
{
servo blue.write(0);
// Position 1 controlled with the angle 0 ° delay(3000); // The program stops for 3 seconds servo blue.write(90); // position 2 controlled with the angle 90 ° delay(3000); // The program stops for 3 seconds servo blue.write(180); // position 3 controlled with the angle 180 ° delay(3000); // The program stops for 3 seconds servo blue.write(20); // position 4 controlled with the angle 20 ° delay(3000); // The program stops for 3 seconds
}
LCD Display
Task: An LCD display is to be controlled with an Arduino microcontroller. After that, a predetermined text should appear as in the following example photo on display.
Material: Microcontroller Board (In this example, UN R3), a wheel (or potentiometer), jumper cables 14, Breadboard
Based on the material and the sketch, you quickly realize that the wiring is not so easy. This is because the LCD display must be connected to a large number of cables. Another difficulty is with which one could connect the cables missing sockets.
Therefore, it is advisable to solder a terminal strip or to solder the cable directly to the LCD. In the second case, it is advisable to use a ribbon cable (for example, from old IDE drives like hard disks, CD or DVD drives). Without a soldered connection, it is almost impossible to achieve good results.
The knob is there to ad the contrast of the LCD. The LED backlight, as shown in the sketch supplied with 5V. Since it would not recognize the label on the LCD in the sketch, it is not shown. You have, therefore, the position of the cable to the LCD counting (Example: The LCD is the first cable from the right GND.
The second cable from the right is connected to 5V, etc.). Info: For quick tinkering, many hobbyists prefer to use an LCD Keypad Shield or I2C LCD because you do not have to worry about the two alternatives to the wiring of the LCD must. However, the two alternatives mentioned above are also more expensive.
If you have successfully made the wiring, you can take the software with many other components, and use is made here to a "Library." The Library for the LCD display is part of the Arduino software and must not be installed separately.
Code:
#include <Liquid Crystal.h>
// Load LCD library
Liquid Crystal LCD (12, 11, 5, 4, 3, 2);
// This line is set,
// which pins is the microcontroller boards used for the LCD (Best
// first do not change). void set up() {
lcd.begin(16, 2);
// In the Setup specifies how many characters and lines are applied. Here: 16 characters in 2 lines
}
void loop() {
lcd.setCursor(0, 0);
Set the LCD // start position of the display.
// Means: The first character in the primal line.
lcd.print("Www.xyz.com");
// There should text
"Www.xyz.com"
//appear.
lcd.setCursor(0, 1);
// lcd.setCursor (0.1) means: First character in the
// second line.
lcd.print("I wish you success...");
// There will be the text "Good luck ..."
//Pop up.
}
A variation: to alternately first up and then down a text message display. In this example, the text "up" and "down".
#include <Liquid Crystal.h> Liquid Crystal LCD (12, 11, 5, 4, 3, 2); void set up() {
lcd.begin(16, 2);
}
void loop() {
lcd.setCursor(0, 0);
// start at the first character in the first line with the
// text "Up." lcd.print("Above");
delay (2000);
Wait // Two seconds. lcd.clear(); // Clear display.
lcd.setCursor(5, 1);
// Renewed beginning at the fifth character in the second
// line with the text "Down". lcd.print("Below");
delay (2000);
Wait // Two seconds. lcd.clear(); // Clear display.
}
An LCD is particularly well suited to sensor values or other issues of microcontroller boards display. For more help, you get, for example, in the Arduino software. Among the sample Sketches, one finds a number of different examples under the menu item "Liquid Crystal."
Relay Card
When tinkering with Arduino relay is very important. Relays are switches that can flow a larger current than would be possible by the microcontroller. With the Arduino, so you now enabled only a very small current relay, which can then also run large electrical items.
The relay card is connected to the Arduino to the contacts (in the photo on the right edge, between the red and green LED). The card is in operation permanently + 5V and GND - connected (). The pin labeled "IN" is connected to a pin of the digital Arduino boards. As long as at pin "IN" no signal (output from the digital pin of Arduino), the screw terminals A and B are connected together. As soon as a signal is present, contacts B and C are connected to each other.
Warning: relay cards that switch when applied to the pin "IN" GND, and there are relay cards that switch when applied to the pin "IN,” a voltage of 5V +. Which version you can easily tell by combining the "signal" Pin once with GND and once with 5V + Arduino boards. A loud crack can clearly see the switching of the relay card.We can connect an electric device, at which a higher electric current flows as if it could provide the Arduino to the bottom contacts (A, B, C). For example, a large electric motor, a large lamp, etc.
As an example code, in this case, the simple "blink" code can be used. In place of the LED, you to close the output pin of the Arduino boards to the "signal" Pin the relay card. The relay will then switch every second.
Code:
void set up()
{
pinMode(6, OUTPUT);
}
void loop()
{
digitalWrite(6, HIGH);
// At this point, would one turn on the relay delay(1000); //... wait a second
digitalWrite(6, LOW);
And off again //
delay(1000);
//... and wait a second.
}
Stepper Motor
In this step, the motor is a stepper motor suitable for small applications with the Arduino board. The special feature is that we can operate it without an external power supply. The motor develops a relatively high torque. This is realized by a transmission, which has been installed within the metal casing before the actual step motor.
This makes it possible at all in this compact design, is that a complete revolution of the driveshaft to 2048 individual steps can be divided. A small resultant disadvantage is the slow maximum rotational speed.
We connect the stepper motor to a motor control board. This supplies the engine with sufficient electrical power so that the power need not apply to the digital pins on the Arduino board. The control board is available in two versions, in which the side-mounted pins are protruding either upward or downward out of the board. However, the pin assignment is the same.
Cabling
PIN2 the Arduino board to IN4 of the motor control board - 2 to 4
PIN 3 on the Arduino board to IN2, the motor control board - 3 to 2
PIN4 the Arduino board to IN3 of the motor control board - 4 to 3
PIN 5 on the Arduino board to IN1 of the motor control board - 5 to 1
PIN "GND" on the Arduino board to GND, the motor control board
PIN "5V" on the Arduino board to VCC, the motor control board
This is an example code that the motor turns by 2048 steps (corresponding to a full revolution) above and can turn back.
Code
#include <stepper.h> int SPMU = 32;
stepper myStepper (SPMU, 2,3,4,5); void set up()
{
myStepper.setSpeed(500);
}
void loop() {MyStepper.step(2048); delay(500); myStepper.step(-2048); delay(500);
}
Instructions for a Humidity Sensor
With a moisture sensor (Moisture sensor), you can, as the name suggests measuring the moisture. However, this refers to the directly adjacent moisture, such as skin moisture or soil moisture, but not from humidity. You can use it, for example, to measure the moisture in the soil of plants. In the case of dryness, then an alarm signal may be heard, or an electric water pump could automatically the plant with water supply.
The sensor is also suitable to measure the water level in the sensor's range. The operation is simple. a voltage is applied to the two contacts of the sensor. The higher the moisture between the two contacts, the better the current to flow to the other from a contact.
This value is processed electronically in the sensor and transmitted to an analog signal on the molded card. Since the card, as described in previous manuals, cannot measure voltage per se, it converts the voltage present on the analog contact to a digital value. From 0 to 12 V corresponds to a digital value of 0 to 1023 (the digits 1024, since zero, is considered first).
In the humidity sensor, however, the upper limit is about at the numerical value of 800 when the sensor is completely immersed in the water. The exact calibration is, however, dependent on the sensor and of the kind that is measured of the liquid/moisture (e.g., saltwater has better conductivity, and the value would be correspondingly higher).
The programming is very simple and very similar to the very reading of potentiometers, as an analog value is read.
int reading = 0;
// Under the variable "reading" is later, the measured value of the
// sensor stored.
void set up()
{
// Here the setup starts.
Serial.begin(9600);
// Communication with the serial port is started.
// This is required to display the value read in the serial monitor to
//to let.
}
void loop()
{
// This brace is the Loop section opened.
reading =analog read(A0);
// The voltage on the sensor is read out and
// under the variable "reading" saved.
Serial.print("Humidity Reading");
// Output on Serial Monitor: The word
// "Humidity Reading"
Serial.println(Measured value);
// and following the actual measured value delay(500); // Finally, a little break, so not too many
// rushing numbers on the Serial Monitor.
}
Expansion of the Program:
Now, an alarm signal with the aid of piezo speakers to sound as soon as the measured humidity falls below a certain limit.
As the limit of the value "200" is set in this case.
Code:
int reading = 0;
int Beep = 6;
// The name "PIEPS" the Pin6 is now referred to which a piezo speaker is connected.
void set up()
{
Serial.begin(9600);
pinMode (6,OUTPUT);
// In the setup of the pin 6 is set as output
}
void loop()
{
reading =analog read(A0); Serial.print("Humidity Reading");
Serial.println(Measured value);
delay(500);
if (Reading <200)
// Here the query begins: When the sensor value is less
// is called "200,” then...
{
digitalWrite(PIEPS, HIGH);
//... to beep the piezo speaker
}
else
//…otherwise…
{
digitalWrite(PIEPS, LOW);
//... he should be quiet.
}
}
Drop Sensor
A drop sensor or liquid sensor, as the name suggests, can detect a liquid. For this purpose, the liquid must be located directly on the sensor. It is enough, a small drop, to get a clear reading.
You can use the sensor, for example, as a rain sensor. Once a drop strikes the sensor, the Arduino board can perform an action such as roll-up an awning, blinds close, trigger an alarm, or actuate a wiper.
The operation is simple. Traverse the sensor at the long contact point is a voltage (either + or -). As soon as a liquid, for example. Connects through a tropical two contacts, a small current flows from
one contact to another. This value is processed electronically in the sensor and transmitted as an analog signal to the analog input of the card in a form.
Since the card cannot measure the voltage per se as described in previous instructions, it converts the voltage applied to the analog pin into a digital value. From 0 to 12 volts corresponds to a digital value of 0 to 1023 (the number 1024 because zero is considered the first value).
In the liquid sensor, the value in the dry is zero "0". When a drop of water hits the contacts of the sensor, the Who at about "480" is located. The more drops are on the sensor, the higher the value.
The first code it's all about, read the Sensorvalue with the Arduino board and display with the "serial monitor."
The programming is very simple and very similar to the very reading of potentiometers or the reading of the humidity sensor, as an analog value is read.
int reading = 0;
// Under the variable "reading" is later, the measured value of the
// sensor stored.
void set up()
{
// Here the setup starts.
Serial.begin(9600);
// Communication with the serial port is started.
// This is required to display the value read in the serial monitor to
//to let.
}
void loop()
{
// This brace is the Loop section opened.
reading =analog read(A0);
// The voltage on the sensor is read out and
// under the variable "reading" saved.
Serial.print("Humidity Reading");
// Output on Serial Monitor: The word
// "Humidity Reading"
Serial.println(Measured value);
// and following the actual measured value delay(500); // Finally, a little break, so not too many
// rushing numbers on the Serial Monitor.
}
Expansion of the Program:
Now an alarm signal using piezo speakers to alert you when a raindrop hits the sensor. As the limit, in this case, the measured value is determined 400, as expected with a drop on the sensor, a measured value of about 480 for sale.
Code:
int reading = 0;
int Beep = 6;
// The name "PIEPS" the Pin6 is now referred to which a piezo speaker is connected.
void set up()
{
Serial.begin(9600);
pinMode (6,OUTPUT);
// In the setup of the pin 6 is set as output
}
void loop()
{
reading =analog read(A0); Serial.print("Humidity Reading"); Serial.println(Measured value);
delay(500);
if (Reading > 400)
// Here the query begins: When the sensor value is less
// is called "400,” then...
{
digitalWrite(PIEPS, HIGH);
//... to beep the piezo speaker
}
else
//…otherwise…
{
digitalWrite(PIEPS, LOW);
//... he should be quiet.
}
}
RFID Kit
The RFID ( "radio frequency identification") reader is used (also called "RFID tags") of RFID transmitters read a particular code by radio. Each transmitter has a unique case, only own individual code. Thus can be realized with the Arduino locking systems or similar projects where a person to identify with a transmitter.
RFID tags can have different forms, such as key fobs or cards in
credit card format.
How does it work? An RFID receiver contains a small copper coil that generates a magnetic field. An RFID transmitter also includes a copper coil that picks up the magnetic field and generates an electric voltage in the transmitter. This voltage is then used to bring a small electronic chip to emit an electrical code by radio. This code is then received directly from the transmitter and processed so that the Arduino microcontroller may further process the received code.
You can also describe RFID tags with a code. This is not taken into account because of the complexity of this manual. Other guides are available online.
Read an RFID tag with Arduino and Process the Data
Material: UN or MEGA Arduino, RFID reader, at least one RFID tag, display plate, display plate cable, LED, 200-ohm resistor.
Task: Using an Arduino microcontroller, an RFID tag to be read. Unless it is the correct TAG, it is to light an LED for 5 seconds.
Wiring the Arduino board with the RFD-receiver:
Board:
|
Arduino Uno
|
Arduino Mega
|
MFRC522 READER
|
Pin code:
|
10
|
53
|
SDA
|
Pin code:
|
13
|
52
|
SCK
|
Pin code:
|
11
|
51
|
MOSI
|
Pin code:
|
12
|
50
|
MISO
|
Pin code:
|
unassigned
|
unassigned
|
IRQ
|
Pin code:
|
GND
|
GND
|
GND
|
Pin code:
|
9
|
5
|
RST
|
Pin code:
|
3.3V
|
3.3V
|
3.3V
|
In this example, the 90° curved contact pins are soldered to the RFID receiver so that the receiver can be placed vertically in a breadboard.
Preparation - the first sketch with the RFID READER
First, we will read the UID ( "Unique Identification Number"), i.e., the individual name of an RFID tag. We use the following program (Attention, the program only works if we added the library to the Arduino software). The program is provided an R3 microcontroller for the UN. In MEGA2560 and other controllers, the pins have to be added accordingly.
#include <SPI.h> // SPI Bibliotheque Add #include <MFRC522.h> // add RFID Bibliotheque #define SS_PIN 10 // SDA to pin 10 (MEGA otherwise) #define RST_PIN 9 // RST to pin 9 (MEGA otherwise)
MFRC522 mfrc522 (SS_PIN, RST_PIN); // RFID receiver name
void set up() // start the setup:
{
Serial,begin(9600); // start serial connection (Monitor)
SPI,begin(); // build SPI connection mfrc522.PCD_Init (); // initialization of the RFID receiver
}
void loop() // Here, the loop part begins
{
if (. Mfrc522.PICC_IsNewCardPresent ()) // If a card within reach
// is...
{
return; // go on...
}
if (. Mfrc522.PICC_ReadCardSerial ()) // When an RFID transmitter was selected
{
return; // go on...
}
Serial,print("The ID of the RFID TAGS is:"); // "The ID of the RFID TAGS
// reads: "is written to the Serial Monitor for (byte i = 0; i <mfrc522.uid.size; i ++)
{
Serial,print(Mfrc522.uid.uidByte [i], HEX); // Then the UID is read,
// each of four blocks is and sequentially to the serial
// Monitor sent. The ending Hex means that the four blocks of the UID
// is (including letters) output as a HEX number
Serial,print(""); // The "Serial.print (" ") command;" ensures that
// stands between the individual blocks read a space.
}
Serial,println(); // This line is on the Serial Monitor only
// line break made.
}
If all is well, the result of the Serial Monitor looks (Apart from the own UID) like this:
HEX number with this written succession does not work well. So we change the line "Serial.print (mfrc522.uid.uidByte [i], HEX)," To
Then one gets as the result of the individual blocks of the UID code is output as a decimal number; "Serial.print (mfrc522.uid.uidByte [i], DEC".
RFID Sketch 2
Now the UID code is being issued as a decimal number, but it is still divided into four blocks. We change the code now with a little math to the effect that we obtain a single contiguous normal number for the UID (decimal).
Why do we do that? If we want to use the sketch later to trigger a correctly read RFID tags (e.g., an LED should light, or a servomotor is to be rotated 90 degrees...), we can better use an IF command with an associated number. For example:
"If the RFID code = is 1031720, then an LED for 5 seconds to light up."
Harder would contrast with the command "If the first block is 195 and the second block 765 reads and the third block 770 blocks 233 and the fourth is... Then turn the LEDs for 5 seconds.
However, a disadvantage is that the sketch is thus somewhat uncertain because all the numbers of the four blocks (max. 12 digits) can not be represented in a continuous number. If there is to be more secure, you would have every single block a query.
#include <SPI.h> #include <MFRC522.h> #define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522 (SS_PIN, RST_PIN);
void set up()
{
Serial.begin(9600); SPI.begin(); mfrc522.PCD_Init ();
}
void loop()
{
if (. Mfrc522.PICC_IsNewCardPresent ())
{
return;
}
if (. Mfrc522.PICC_ReadCardSerial ())
{
return;
}
long code = 0; // As a new variable we add "code" under which the UID later is output as a continuous number. Instead of int, we now use the number range "long" because they can store a larger number.
for (byte i = 0; i <mfrc522.uid.size; i ++)
{
code = ((code + mfrc522.uid.uidByte [i]) * 10); // Now, as also previously read out the four blocks, and in each pass of the code is "stretched" by a factor of 10 degrees. (, here you would use the value in 1000, but the number would become too large.
}
Serial.print("The card number is ::"); // Finally, the number code (you can not call him as a UID more) output.
Serial.println(code);
}
Great, now we can read a unique identification number of an RFID tag (it displays The number on the Serial Monitor). In this case, the identification number of that individual RFID tags 1031720 is.
And how does it continue? Now we want to turn on an LED for 5 seconds if it holds the desired RFID tag in front of the RFID READER.
Sketch 3
#include <SPI.h> #include <MFRC522.h> #define SS_PIN 10
#define RST_PIN 9
MFRC522 mfrc522 (SS_PIN, RST_PIN);
void set up()
{
Serial.begin(9600); SPI.begin(); mfrc522.PCD_Init ();
pinMode (2, OUTPUT); // The pin 2 is now an output (Connects a LED)
}
void loop()
{
if (. Mfrc522.PICC_IsNewCardPresent ())
{
return;
}
if (. Mfrc522.PICC_ReadCardSerial ())
{
return;
}
long code = 0;
for (byte i = 0; i <mfrc522.uid.size; i ++)
{
code = ((code + mfrc522.uid.uidByte [i]) * 10);
}
Serial.print("The card number is"); Serial.println(code);
// From here takes place the expansion of the program.
if (Code == 1031720) // If the number code is 1031720...
{ // Open Programmabschniss
digitalWrite (2, HIGH); should //...dann the LED on pin 2 light... delay (5000); // for 5 seconds
digitalWrite (2, LOW); //... and then go out again
} // close the program section
} Completing // Sketch