arduino:arduino_crash_course:misc
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
arduino:arduino_crash_course:misc [2015/02/15 04:47] – mithat | arduino:arduino_crash_course:misc [2015/02/16 04:22] (current) – [Serial communication] mithat | ||
---|---|---|---|
Line 19: | Line 19: | ||
* **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." | ||
- | ===== I/O config ===== | + | ==== "Most preferred" |
- | * "The analog input pins can be used as digital pins, referred to as A0, A1, etc." ((http://arduino.cc/en/ | + | Special functions are consumed by: |
+ | |||
+ | * **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 ==== | ||
- | ==== Binary | + | === Digital |
* 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 | + | * 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 |
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); |
} | } | ||
</ | </ | ||
- | ==== PWM output | + | * "The analog input pins can be used as digital pins, referred to as A0, A1, etc." ((http:// |
+ | * "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:// | ||
<code c> | <code c> | ||
- | const int LED = 11; /* 3,5,6,9,10,11 on Uno. */ | + | pinMode(A0, OUTPUT); |
+ | digitalWrite(A0, HIGH); | ||
+ | </code> | ||
- | setup() { | + | === PWM output === |
- | | + | * PWM around 490 or 980 Hz. |
- | } | + | * "You do not need to call pinMode() to set the pin as an output before calling analogWrite()." |
+ | | ||
+ | |||
+ | <code c> | ||
+ | const int LED_PIN = 11; /* 3, | ||
loop() { | loop() { | ||
- | analogWrite(LED, /*0 to 255*/); | + | analogWrite(LED_PIN, 127); /* 0 to 255. */ |
} | } | ||
</ | </ | ||
- | ==== Binary input ==== | + | ==== Input ==== |
- | * " | + | |
+ | === Digital input === | ||
+ | * " | ||
* Internal pullup resistors are disabled by default. | * Internal pullup resistors are disabled by default. | ||
Line 67: | Line 98: | ||
btnState = digitalRead(BUTTON); | btnState = digitalRead(BUTTON); | ||
} | } | ||
+ | </ | ||
+ | |||
+ | * "The analog input pins can be used as digital pins, referred to as A0, A1, etc." ((http:// | ||
+ | * "The analog pins can be used identically to the digital pins, using the aliases A0 (for analog input 0), A1, etc." ((http:// | ||
+ | |||
+ | <code c> | ||
+ | pinMode(A0, INPUT); | ||
+ | btnState = digitalRead(A0); | ||
</ | </ | ||
Line 80: | Line 119: | ||
</ | </ | ||
- | ==== 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 '' | ||
+ | * 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); | ||
+ | } | ||
</ | </ | ||
+ | ---- | ||
+ | |||
+ | ===== Serial communication ===== | ||
+ | |||
+ | * Serial I/O consumes pins 0 (for RX) and 1 (for TX) if used. | ||
+ | * [[http:// | ||
+ | |||
+ | ==== Serial output ==== | ||
+ | |||
+ | Simple reporting: | ||
+ | <code c> | ||
+ | void setup() { | ||
+ | Serial.begin(9600); | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | Serial.write(65); | ||
+ | Serial.print(42); | ||
+ | Serial.println(" | ||
+ | Serial.print(78, | ||
+ | Serial.println(1.23456, | ||
+ | Serial.end(); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Serial output ==== | ||
+ | Later. | ||
+ | |||
+ | |||
+ | ---- | ||
arduino/arduino_crash_course/misc.1423975665.txt.gz · Last modified: 2015/02/15 04:47 by mithat