User Tools

Site Tools


arduino:arduino_crash_course:misc

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
arduino:arduino_crash_course:misc [2015/02/15 04:42] mithatarduino:arduino_crash_course:misc [2015/02/16 04:22] (current) – [Serial communication] mithat
Line 2: Line 2:
  
 ===== Uno hardware ===== ===== Uno hardware =====
-Each of the 14 digital pins on the Uno can be used as an input or output, using ''pinMode()'', ''digitalWrite()'', and ''digitalRead()'' functions. They operate at 5 volts. Each pin can provide or receive a maximum of 40 mA and has an internal pull-up resistor (disconnected by default) of 20-50 kOhms. In addition, some pins have specialized functions:+"Each of the 14 digital pins on the Uno can be used as an input or output, using ''pinMode()'', ''digitalWrite()'', and ''digitalRead()'' functions. They operate at 5 volts. Each pin can provide or receive a maximum of 40 mA and has an internal pull-up resistor (disconnected by default) of 20-50 kOhms. In addition, some pins have specialized functions:
  
   * **Serial: 0 (RX) and 1 (TX).** Used to receive (RX) and transmit (TX) TTL serial data. These pins are connected to the corresponding pins of the ATmega8U2 USB-to-TTL Serial chip.   * **Serial: 0 (RX) and 1 (TX).** Used to receive (RX) and transmit (TX) TTL serial data. These pins are connected to the corresponding pins of the ATmega8U2 USB-to-TTL Serial chip.
Line 10: Line 10:
   * **LED: 13.** There is a built-in LED connected to digital pin 13. When the pin is HIGH value, the LED is on, when the pin is LOW, it's off.    * **LED: 13.** There is a built-in LED connected to digital pin 13. When the pin is HIGH value, the LED is on, when the pin is LOW, it's off. 
  
-The Uno has 6 analog inputs, labeled ''A0'' through ''A5'', each of which provide 10 bits of resolution (i.e. 1024 different values). By default they measure from ground to 5 volts, though is it possible to change the upper end of their range using the AREF pin and the ''analogReference()'' function. Additionally, some pins have specialized functionality:+"The Uno has 6 analog inputs, labeled ''A0'' through ''A5'', each of which provide 10 bits of resolution (i.e. 1024 different values). By default they measure from ground to 5 volts, though is it possible to change the upper end of their range using the AREF pin and the ''analogReference()'' function. Additionally, some pins have specialized functionality:
  
   * **TWI: A4 or SDA pin and A5 or SCL pin.** Support TWI communication using the Wire library.    * **TWI: A4 or SDA pin and A5 or SCL pin.** Support TWI communication using the Wire library. 
  
-There are a couple of other pins on the board:+"There are a couple of other pins on the board:
  
   * **AREF.** Reference voltage for the analog inputs. Used with analogReference().   * **AREF.** Reference voltage for the analog inputs. Used with analogReference().
-  * **Reset.** Bring this line LOW to reset the microcontroller. Typically used to add a reset button to shields which block the one on the board. +  * **Reset.** Bring this line LOW to reset the microcontroller. Typically used to add a reset button to shields which block the one on the board." ((http://arduino.cc/en/Main/ArduinoBoardUno))
  
-===== I/O config ===== +==== "Most preferred" pins ==== 
-  * "The analog input pins can be used as digital pins, referred to as A0, A1, etc."((http://arduino.cc/en/Reference/PinMode))+Special functions are consumed by:
  
-==== Binary output ====+  * **I2C (TWI):** A4 and A5. 
 +  * **SPI:** 10, 11, 12, 13. 
 +  * **Interrupts:** 2, 3. 
 +  * **Serial:** 0, 1 
 + 
 +This leaves: 
 + 
 +  * **Digital out/in:** 4, 7, 8 
 +  * **PWM out:** 9, 10, and 11; pins 5 and 6 have higher-than-expected duty cycles. 
 +  * **Analog or digital out/in:** A0, A1, A2, A3 
 + 
 +Note that pin 13 also has a pullup resistor on it for the internal LED, meaning it's a poor choice for use as an input even if you're not using SPI. 
 + 
 +---- 
 + 
 + 
 +===== I/O ===== 
 + 
 +==== Output ==== 
 + 
 +=== Digital output ===
   * Output is limited to 40mA. More current draw can destroy the chip.   * Output is limited to 40mA. More current draw can destroy the chip.
-  * There are no output resistors except on pin 13 (Uno)+  * There are no output resistors
 +    * Some sources suggest that pin 13 on the Uno has a resistor. Examining the schematic suggests that the resistor is only connected to the internal LED. Pin 13 is naked.
  
 <code c> <code c>
-const int LED = 13;+const int LED_PIN = 13;
  
 setup() { setup() {
-  pinMode(LED, OUTPUT);+  pinMode(LED_PIN, OUTPUT);
 } }
  
 loop() { loop() {
-  digitalWrite(LED, HIGH); +  digitalWrite(LED_PIN, HIGH); 
-  digitalWrite(LED, LOW);+  digitalWrite(LED_PIN, LOW);
 } }
 </code> </code>
  
-==== PWM output ====+  * "The analog input pins can be used as digital pins, referred to as A0, A1, etc." ((http://arduino.cc/en/Reference/PinMode)) 
 +  * "The analog pins can be used identically to the digital pins, using the aliases A0 (for analog input 0), A1, etc.  For example, the code would look like this to set analog pin 0 to an output, and to set it HIGH: " ((http://arduino.cc/en/Tutorial/AnalogInputPins)) 
 <code c> <code c>
-const int LED = 11/* 3,5,6,9,10,11 on Uno. */+pinMode(A0, OUTPUT); 
 +digitalWrite(A0HIGH); 
 +</code>
  
-setup() { +=== PWM output === 
-  pinMode(LED, OUTPUT); +  * PWM around 490 or 980 Hz. 
-}+  * "You do not need to call pinMode() to set the pin as an output before calling analogWrite()." ((http://arduino.cc/en/Reference/AnalogWrite)) 
 +  * "The PWM outputs generated on pins 5 and 6 will have higher-than-expected duty cycles." ((http://arduino.cc/en/Reference/AnalogWrite)
 + 
 +<code c> 
 +const int LED_PIN = 11;      /* 3,5,6,9,10,11 on Uno. */
  
 loop() { loop() {
-  analogWrite(LED, /*0 to 255*/);+  analogWrite(LED_PIN127); /* 0 to 255*/
 } }
 </code> </code>
  
-==== Binary input ==== +==== Input ==== 
-  * "Arduino (Atmega) pins default to inputs. Approx load in 100M ohm."((http://arduino.cc/en/Tutorial/DigitalPins))+ 
 +=== Digital input === 
 +  * "Arduino (Atmega) pins default to inputs. Approx load is 100M ohm." ((http://arduino.cc/en/Tutorial/DigitalPins))
   * Internal pullup resistors are disabled by default.   * Internal pullup resistors are disabled by default.
  
Line 67: Line 98:
   btnState = digitalRead(BUTTON);   btnState = digitalRead(BUTTON);
 } }
 +</code>
 +
 +  * "The analog input pins can be used as digital pins, referred to as A0, A1, etc." ((http://arduino.cc/en/Reference/PinMode))
 +  * "The analog pins can be used identically to the digital pins, using the aliases A0 (for analog input 0), A1, etc." ((http://arduino.cc/en/Tutorial/AnalogInputPins))
 +
 +<code c>
 +pinMode(A0, INPUT);
 +btnState = digitalRead(A0);
 </code> </code>
  
Line 74: Line 113:
 "The value of this pullup depends on the microcontroller used. On most AVR-based boards, the value is guaranteed to be between 20kΩ and 50kΩ. On the Arduino Due, it is between 50kΩ and 150kΩ. "The value of this pullup depends on the microcontroller used. On most AVR-based boards, the value is guaranteed to be between 20kΩ and 50kΩ. On the Arduino Due, it is between 50kΩ and 150kΩ.
  
-"Digital pin 13 is harder to use as a digital input than the other digital pins because it has an LED and resistor attached to it that's soldered to the board on most boards. ... If you must use pin 13 as a digital input, set its pinMode() to INPUT and use an external pull down resistor."((http://arduino.cc/en/Tutorial/DigitalPins))+"Digital pin 13 is harder to use as a digital input than the other digital pins because it has an LED and resistor attached to it that's soldered to the board on most boards. ... If you must use pin 13 as a digital input, set its pinMode() to INPUT and use an external pull down resistor." ((http://arduino.cc/en/Tutorial/DigitalPins))
  
 <code c> <code c>
Line 80: Line 119:
 </code> </code>
  
-==== A/D input ====+=== A/D input === 
 +  * 10-bit unsigned A/D conversion with about 10kHz sample rate. 
 +  * Default reference to 5VDC. Can be overridden via ''analogReference();''
 +  * No setup is needed. 
 <code c> <code c>
 +int ANALOG_PIN = 3;  // Maps to A3 we think.
 +int val = 0;
 +
 +void loop() {
 +  val = analogRead(ANALOG_PIN);    // read the input pin
 +}
 </code> </code>
  
  
 +----
 +
 +===== Serial communication =====
 +
 +  * Serial I/O consumes pins 0 (for RX) and 1 (for TX) if used.
 +  * [[http://arduino.cc/en/Reference/Serial|Serial reference]]
 +
 +==== Serial output ====
 +
 +Simple reporting:
 +<code c>
 +void setup() {
 +  Serial.begin(9600);  // init serial communication at 9600 bps
 +}
 +
 +void loop() {
 +  Serial.write(65);           // write a byte (interpreted as ASCII)
 +  Serial.print(42);           // print a number
 +  Serial.println("Yo.");      // print a string as a line
 +  Serial.print(78, HEX);      // print "4E"
 +  Serial.println(1.23456, 4); // print "1.2346"
 +  Serial.end();               // release pins 0 and 1 for general use
 +}
 +</code>
 +
 +==== Serial output ====
 +Later.
 +
 +
 +----
  
  
arduino/arduino_crash_course/misc.1423975347.txt.gz · Last modified: 2015/02/15 04:42 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki