Xathrya Sabertooth |
- Arduino Project: Toggle the LED with Push Button
- Arduino Project: Push Button
- Arduino Project: Software Serial Communication
- Arduino Project: Turn On/Off LED using Serial Communication
- Arduino Project: Simple Serial Communication
Arduino Project: Toggle the LED with Push Button Posted: 27 Mar 2014 06:13 PM PDT This project is upgrade of previous project (Simple Push Button). In this project, we just need to press the push button once to turn it on or off, instead of hold it. Objective
PrerequisiteRequirementFollowing is all materials I use for this tutorial.
You can change the Arduino board to higher specification if you like. The LED I use will emit red light as it is powered by electricity. There are some kind of push button exists. In our project, we need only push button with four legs. You also need an USB A to B cable for upload our program to our Arduino and also for serial communication. CircuitHere is the circuit image, created using Fritzing. To build the circuit, attach the LED as shown in the figure. Attach the long leg of an LED (positif leg or anode) to pin 13. Attach the short leg (negative leg or cathode) to the ground (GND). On Arduino Uno, there is also a built in status LED which connected to pin 13. When our Arduino running successfully, both status LED and external LED will start to blink. The push button has four legs (or four pins) as shown above. Two opposite pins connect when the button is pushed; otherwise, they are disconnected. In one side, we connect one leg to Arduino pin 7. In other side, we supply 5V electricity to one leg. The other leg is connected to a 10k ohm resistor before going to ground. CodeLet’s see what code we use for this project and discuss it later. const unsigned int BUTTON_PIN = 7; const unsigned int LED_PIN = 13; int old_button_state = LOW; int led_state = LOW; void setup() { pinMode(LED_PIN, OUTPUT); pinMode(BUTTON_PIN, INPUT); } void loop() { const int CURRENT_BUTTON_STATE = digitalRead(BUTTON_PIN); if (CURRENT_BUTTON_STATE != old_button_state && CURRENT_BUTTON_STATE == HIGH) { led_state = (led_state == LOW ? HIGH : LOW); digitalWrite(LED_PIN, led_state); delay(50); } old_button_state = CURRENT_BUTTON_STATE; }
Notice that we define two constant here, LED_PIN which is for LED, and BUTTON_PIN for the push button. Both are defined as different purpose, the LED is output pin while the push button is input. There is no significance change to our code from previous project. Things we can notice are the existence of two new variables: old_button_state and led_state. ResultYou should see that LED is turned on or off whenever we press the button. Why it WorksWe define a global variable named led_state to store the current state of our LED. It will be LOW when the LED is on and HIGH otherwise. In loop() we check the button’s current state. When we press the button, the switches is in HIGH logic and we toggle the content of led_state. The global variable old_button_state is used to store the old state of push button. If we don’t do this, when we press and hold the button, the state will constantly be HIGH and we’d toggle the LED’s state continuously. Because this happens so fast it seems like the LED’s constantly on. When we release the button, the LED is in a more or less arbitrary state. Therefore we use previous push button to guard the state of LED.
|
Posted: 27 Mar 2014 05:17 PM PDT Pushbutton is one of common component found in projects. The mechanism is simple, we press push button and we got a signal, otherwise it (should) gives nothing. Objective
PrerequisiteRequirementFollowing is all materials I use for this tutorial.
You can change the Arduino board to higher specification if you like. The LED I use will emit red light as it is powered by electricity. There are some kind of push button exists. In our project, we need only push button with four legs. You also need an USB A to B cable for upload our program to our Arduino and also for serial communication. CircuitHere is the circuit image, created using Fritzing. To build the circuit, attach the LED as shown in the figure. Attach the long leg of an LED (positif leg or anode) to pin 13. Attach the short leg (negative leg or cathode) to the ground (GND). On Arduino Uno, there is also a built in status LED which connected to pin 13. When our Arduino running successfully, both status LED and external LED will start to blink. The push button has four legs (or four pins) as shown above. Two opposite pins connect when the button is pushed; otherwise, they are disconnected. In one side, we connect one leg to Arduino pin 7. In other side, we supply 5V electricity to one leg. The other leg is connected to a 10k ohm resistor before going to ground. CodeLet’s see what code we use for this project and discuss it later. const unsigned int BUTTON_PIN = 7; const unsigned int LED_PIN = 13; void setup() { pinMode(LED_PIN, OUTPUT); pinMode(BUTTON_PIN, INPUT); } void loop() { const int BUTTON_STATE = digitalRead(BUTTON_PIN); if (BUTTON_STATE == HIGH) { digitalWrite(LED_PIN, HIGH); } else { digitalWrite(LED_PIN, LOW); } } Notice that we define two constant here, LED_PIN which is for LED, and BUTTON_PIN for the push button. Both are defined as different purpose, the LED is output pin while the push button is input. Our code is as simple as checking the push button state and then output the corresponding voltage to LED. ResultYou should see that LED is turned on whenever we press the push button and turned off when we don’t. To keep the LED turned on, we have to keep pressing the push button. Why it WorksWhy do we need resistor for push button? The problem is that we expect the push button to return a default value (LOW) in case it isn't pressed. But when the button isn't pressed,it would not be directly connected to ground and would flicker because of static and interference. A little bit of current flows through the resistor,and this helps prevent random noise from changing the voltage that the input pin sees. When the button is pressed, there will still be 5 volts at Arduino’s digital pin, but when the button isn’t pressed, it will cleanly read the connection to the ground. We call this a pull-down resistor. That is, we have to connect the Arduino’s signal pin to power through the pushbutton and connect the other pin of pushbutton to ground using a resistor. You can also wire this circuit the opposite way, with a pullup resistor keeping the input HIGH, and going LOW when the button is pressed. If so, the behavior of the sketch will be reversed, with the LED normally on and turning off when you press the button. |
Arduino Project: Software Serial Communication Posted: 27 Mar 2014 08:04 AM PDT In most case, our device are stand-alone application, which means we don’t involve any additional computer for operation. However, we can still make Arduino interact with our computer in such nice way. We use serial communication to achieve this. Most possible reason for communication is debugging purpose. We analyze Arduino state for various input and condition given, by reading output shown by Arduino. The serial communication can also be used for communication of two devices. Arduino have built-in support for serial communication on pin 0 and 1, but what if we need more serial ports? Here come SoftwareSerial Library to rescue. This library has been developed to allow serial communication to take place on the other digital pins of our Arduino board, using software to replicate the functionality of the hardwired RX and TX lines. This can be extremely helpful when the need arises to communicate with two serial enabled devices, or to talk with just one device while leaving the main serial port open for debugging purpose. Objective
PrerequisiteRequirementFollowing is all materials I use for this tutorial.
You can change the Arduino board to higher specification if you like. You also need an USB A to B cable for upload our program to our Arduino and also for serial communication. CircuitHere is the circuit image, created using Fritzing. We don’t need any extra component to do this project. CodeLet’s see what code we use for this project and discuss it later. #include <SoftwareSerial.h> SoftwareSerial EuSerial(10, 11); // Bind RX and TX to pin 10 and 11 void setup() { // Open serial communications and wait for port to open Serial.begin(57600); // Wait for serial port to connect. Needed for Leonardo only while(!Serial); Serial.println("Arduino SoftwareSerial Demo"); // Set the data rate for the SoftwareSerial port EuSerial.begin(4800); EuSerial.println("Hello, world?"); } void loop() { // Echo data from virtual RX to main TX if (EuSerial.available()) Serial.write(EuSerial.read()); // Echo data from main RX to virtual TX if (Serial.available()) EuSerial.write(Serial.read()); } Here we have two serial line: Arduino serial and virtual serial by SoftwareSerial. Both have difference baud rate, the main lines have baud rate 57600 while the virtual lines have baud rate 4800. The initialization of both lines are similar. We use begin() function to set the baud rate of both lines. In loop(), we will echo every input we got. To do that, we should check the availability first. This is done by executing available(). To read data, Arduino use read() function. To send data to other end, Arduino provide print() and println() function. These functions are slight different where println() send extra new line character at the end of transmission. ResultYou need two serial lines connected to your PC. The result should be similar to Simple Serial Communication project. It is strongly recommended to use PuTTY and connect to both port recognized by your computer. Why it WorksWe have demonstrate serial communication in this project. The word serial means “one after the other”. Serial data transfer is a transmission method where data is transferred one bit at a time, one right after the other. Information is passed back & forth between the computer and Arduino, by essentially, setting a pin high or low. There are two condition of logic in circuit, HIGH and LOW. Some microcontrollers might define HIGH and LOW difference, but in Arduino Uno we have HIGH logic as 5V and LOW logic as 0V. If you know boolean value, yes this is boolean value with HIGH is true and LOW is false. |
Arduino Project: Turn On/Off LED using Serial Communication Posted: 27 Mar 2014 07:34 AM PDT Objective
PrerequisiteRequirementFollowing is all materials I use for this tutorial.
You can change the Arduino board to higher specification if you like. The LED I use will emit red light as it is powered by electricity. You also need an USB A to B cable for upload our program to our Arduino and also for serial communication. CircuitHere is the circuit image, created using Fritzing. To build the circuit, attach the LED as shown in the figure. Attach the long leg of an LED (positif leg or anode) to pin 13. Attach the short leg (negative leg or cathode) to the ground (GND). On Arduino Uno, there is also a built in status LED which connected to pin 13. When our Arduino running successfully, both status LED and external LED will start to blink. We don’t need extra resistor for this project, but make sure you are using pin 13. If you do connect it to other pin, it might be destroyed. The reason is that pin 13 has an internal resistor that other pins don’t have which limit current to our LED. CodeLet’s see what code we use for this project and discuss it later. const unsigned int LED_PIN = 13; const unsigned int BAUD_RATE = 9600; char command; void setup() { pinMode(LED_PIN, OUTPUT); Serial.begin(BAUD_RATE); } void loop() { if (Serial.available() > 0) { command = Serial.read(); if (command == '1') { Serial.println("Led on"); digitalWrite(LED_PIN, HIGH); } else if (command == '2') { Serial.println("Led off"); digitalWrite(LED_PIN, LOW); } else { Serial.println("Led pattern 1"); digitalWrite(LED_PIN, HIGH); delay(250); digitalWrite(LED_PIN, LOW); delay(500); digitalWrite(LED_PIN, HIGH); delay(250); digitalWrite(LED_PIN, LOW); } Serial.print("You enter: "); Serial.println(command); } } Notice that we define two constant here, LED_PIN which is pin we use, and BAUD_RATE for the baud rate. Baud rate is the rate of character transferred every second. Notice that 1 character is 1 byte, which is also 8 bit. Both party who communicate must agree on same baud rate before transmission began. If baud rate is not equal, data can’t be transferred correctly. Here we introduce a object called Serial. This object handles input output in serial communication. To use this object we should initialize which baud rate we use. In this case we use 9600, which is common baud rate used. To do input, we should check the availability first. This is done by executing Serial.available(). Single character can be read by read() function. To send data to other end, Arduino provide print() and println() function. These functions are slight different where println() send extra new line character at the end of transmission. We define three commands: ’1′, ’2′, and ’3′. If we enter 1, we turn the LED on. If we enter 2, we turn the LED off. If we enter 3, we make the LED on/off in certain pattern. ResultOnce the program is uploaded, open “Serial Monitor” window in Arduino IDE. It is our tool to do serial communication using Arduino IDE. You can open it by navigate to Tool > Serial Monitor. Another way to do it is press CTRL + SHIFT + M at once. To give input to Arduino, type in to the text box and click on “Send” button. Now give input of number 1, 2, 3 and see the result. You should see the LED are turned on and off according to your command. Why it WorksIt similar to project Simple Serial Communication where we received some commands from serial communication. This command are then processed to determine which routine should we do. |
Arduino Project: Simple Serial Communication Posted: 27 Mar 2014 06:27 AM PDT In most case, our device are stand-alone application, which means we don’t involve any additional computer for operation. However, we can still make Arduino interact with our computer in such nice way. We use serial communication to achieve this. Most possible reason for communication is debugging purpose. We analyze Arduino state for various input and condition given, by reading output shown by Arduino. The serial communication can also be used for communication of two devices. Objective
Prerequisite
RequirementFollowing is all materials I use for this tutorial.
You can change the Arduino board to higher specification if you like. You also need an USB A to B cable for upload our program to our Arduino and also for serial communication. CircuitHere is the circuit image, created using Fritzing. We don’t need any extra component to do this project. CodeLet’s see what code we use for this project and discuss it later. const unsigned int BAUD_RATE = 9600; int a = 5; int b = 10; int c = 20; void setup() { Serial.begin(BAUD_RATE); Serial.println("Arduino Serial Communication Demo"); Serial.print("a = "); Serial.println(a); Serial.print("b = "); Serial.println(b); Serial.print("c = "); Serial.println(c); Serial.print("a + b = "); // addition Serial.println(a + b); Serial.print("a * c = "); // multiplication Serial.println(a * c); Serial.print("c / b = "); // division Serial.println(c / b); Serial.print("b - c = "); // subtract Serial.println(b - c); } void loop() { if (Serial.available()) { Serial.write(Serial.read()); } } In the first line we declare a constant, BAUD_RATE, which has value 9600. Baud rate is the rate of character transferred every second. Notice that 1 character is equal to 1 byte or 8 bit. Both party who communicate using serial communication must agree on same baud rate before transmission began. If baud rate is not equal, data can’t be transferred correctly. Here we introduce a object called Serial. This object handles input output in serial communication. To use this object we should initialize which baud rate we use. In this case we use 9600, which is common baud rate used. We declare three variables for our math operation: a, b, and c with their respective value. In setup() we do some basic math operation over these three variables. The result will be presented every time we initiate serial connection. In loop(), we will echo every input we got. To do that, we should check the availability first. This is done by executing available(). To read data, Arduino use read() function. To send data to other end, Arduino provide print() and println() function. These functions are slight different where println() send extra new line character at the end of transmission. ResultOnce the program is uploaded, open “Serial Monitor” window in Arduino IDE. It is our tool to do serial communication using Arduino IDE. You can open it by navigate to Tool > Serial Monitor. Another way to do it is press CTRL + SHIFT + M at once. Here we will be presented with the result of our math operation. To give input to Arduino, type in to the text box and click on “Send” button. You will see nice similar message you type. Here is the result after I type “Satria Ady Pradana”: Alternatively, you can use puTTY in Windows or Linux terminal in Linux. However we won’t cover this subject. Why it WorksWe have demonstrate serial communication in this project. The word serial means “one after the other”. Serial data transfer is a transmission method where data is transferred one bit at a time, one right after the other. Information is passed back & forth between the computer and Arduino, by essentially, setting a pin high or low. There are two condition of logic in circuit, HIGH and LOW. Some microcontrollers might define HIGH and LOW difference, but in Arduino Uno we have HIGH logic as 5V and LOW logic as 0V. If you know boolean value, yes this is boolean value with HIGH is true and LOW is false. |
You are subscribed to email updates from Xathrya Sabertooth To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 20 West Kinzie, Chicago IL USA 60610 |
Tidak ada komentar:
Posting Komentar