This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
digispark:tutorials:basics [2013/02/08 02:56] marhar |
digispark:tutorials:basics [2016/06/09 12:03] (current) |
||
|---|---|---|---|
| Line 10: | Line 10: | ||
| - The Digispark is powered by an [[http://www.atmel.com/devices/attiny85.aspx|Atmel Attiny85 MCU]] - this has many differences from an Arduino's ATmega328 and some libraries may not work correctly on it. | - The Digispark is powered by an [[http://www.atmel.com/devices/attiny85.aspx|Atmel Attiny85 MCU]] - this has many differences from an Arduino's ATmega328 and some libraries may not work correctly on it. | ||
| - | - The Digispark only has about 6k of flash memory for storing your code. | + | - The Digispark only has about 6 KB of flash memory for storing your code. |
| - Pin 3 and Pin 4 (P3 and P4) are used for USB communication and programming, while you can use them in your circuit if you are not using USB communication, you may have to unplug your circuit during programming if the circuit would impede the pin states or dramatically affect the voltage levels on these pins. | - Pin 3 and Pin 4 (P3 and P4) are used for USB communication and programming, while you can use them in your circuit if you are not using USB communication, you may have to unplug your circuit during programming if the circuit would impede the pin states or dramatically affect the voltage levels on these pins. | ||
| - | - Pin 3 (P3) has a 1.5k pull-up attached to it which is required for when P3 and P4 are used for USB communication (including programming). Your design may need to take into account that you'd have to overpower this to pull this pin low. | + | - Pin 3 (P3) has a 1.5 kΩ pull-up resistor attached to it which is required for when P3 and P4 are used for USB communication (including programming). Your design may need to take into account that you'd have to overpower this to pull this pin low. |
| - The Digispark does not have a hardware serial port nor a hardware serial to USB converter. An example library (DigiUSB) is provided, as well as some example code and a serial monitor like program, but communication with the computer will not always be plug and play, especially when other libraries are involved. | - The Digispark does not have a hardware serial port nor a hardware serial to USB converter. An example library (DigiUSB) is provided, as well as some example code and a serial monitor like program, but communication with the computer will not always be plug and play, especially when other libraries are involved. | ||
| ===== DigitalWrite: ===== | ===== DigitalWrite: ===== | ||
| - | |||
| - | |||
| <code> | <code> | ||
| void setup() { | void setup() { | ||
| - | //All pins are capable of Digital output, though P5 is 3v at HIGH instead of 5v | + | //All pins are capable of Digital output, though P5 is 3 V at HIGH instead of 5 V |
| - | pinMode(0, OUTPUT); //0 is P0, 1 is P1, 2 is P2, etc - unlike the analog inputs, for digital outputs the pin number matches | + | pinMode(0, OUTPUT); //0 is P0, 1 is P1, 2 is P2, etc. - unlike the analog inputs, for digital outputs the pin number matches. |
| } | } | ||
| void loop() { | void loop() { | ||
| - | digitalWrite(0,HIGH); //turn the pin HIGH (5v) | + | digitalWrite(0,HIGH); //Turn the pin HIGH (5 V) |
| - | delay(1000); | + | delay(1000); |
| - | digitalWrite(0,HIGH); //turn the pin LOW (GND) | + | digitalWrite(0,LOW); //Turn the pin LOW (GND) |
| - | delay(1000); | + | delay(1000); |
| } | } | ||
| </code> | </code> | ||
| - | |||
| ===== Digital Read: ===== | ===== Digital Read: ===== | ||
| - | **NOTE:** The internal pull-up (turned on by calling digitalWrite(0) after setting the pin to output, where 0 is the pin number) are much weaker (about 25k ohm) on an attiny then on an Arduino so the onboard LED interferes with them. If you need them you can use a different port, change your circuit to not need the internal pull-up, or cut the LED trace. For Model A this would apply to P1 for Model B this would apply to P0.([[digispark:tutorials:modelbi2c|Model Identification]]) | + | **NOTE:** The internal pull-up resistor (turned on by calling digitalWrite(0) after setting the pin to output, where 0 is the pin number) are much weaker (about 25 kohm) on an ATtiny than on an Arduino, so the onboard LED interferes with them. If you need them, you can use a different port. Change your circuit to not need the internal pull-up, or cut the LED trace. For Model A this would apply to P1 for Model B this would apply to P0.([[digispark:tutorials:modelbi2c|Model Identification]]) |
| Line 43: | Line 40: | ||
| void setup() { | void setup() { | ||
| - | //All pins are capable of Digital input | + | //All pins are capable of digital input. |
| - | pinMode(0, INPUT); //0 is P0, 1 is P1, 2 is P2, etc - unlike the analog inputs, for digital inputs the pin number matches | + | pinMode(0, INPUT); //0 is P0, 1 is P1, 2 is P2, etc. - unlike the analog inputs, for digital inputs the pin number matches. |
| } | } | ||
| void loop() { | void loop() { | ||
| - | sensorValue = digitalRead(1); //returns HIGH or LOW (true or false / 1 or 0) | + | sensorValue = digitalRead(1); //Returns HIGH or LOW (true or false / 1 or 0). |
| } | } | ||
| </code> | </code> | ||
| Line 57: | Line 54: | ||
| void setup() { | void setup() { | ||
| - | //You need not set pin mode for analogRead - | + | //You need not set pin mode for analogRead - though if you have set the pin to |
| - | //though if you have set the pin to output and later want to read from it then you need to set //pinMode(0,INPUT); where 0 is the physical pin number not the analog input number. | + | //output and later want to read from it then you need to set pinMode(0,INPUT); |
| - | //see below for the proper pinMode statement to go with each analog read | + | //where 0 is the physical pin number not the analog input number. |
| + | // | ||
| + | //See below for the proper pinMode statement to go with each analog read. | ||
| } | } | ||
| void loop() { | void loop() { | ||
| - | // The analog pins are referenced by their analog port number, not their pin number and are as follows: | + | // The analog pins are referenced by their analog port number, not their pin |
| + | //number and are as follows: | ||
| - | sensorValue = analogRead(1); //read P2 | + | sensorValue = analogRead(1); //Read P2 |
| - | //to set to input: pinMode(2,INPUT); | + | //To set to input: pinMode(2, INPUT); |
| - | //THIS IS P2, P2 is Analog Input 1, so when you are using analog read you refer to it as 1 | + | //THIS IS P2, P2 is analog input 1, so when you are using analog read, you refer to it as 1. |
| - | //sensorValue = analogRead(2); //read P4 | + | //sensorValue = analogRead(2); //Read P4 |
| - | //to set to input: pinMode(4,INPUT); | + | //To set to input: pinMode(4, INPUT); |
| - | //THIS IS P4, P2 is Analog Input 2, so when you are using analog read you refer to it as 2 | + | //THIS IS P4, P4 is analog input 2, so when you are using analog read, you refer to it as 2. |
| - | //sensorValue = analogRead(3); //read P3 | + | //sensorValue = analogRead(3); //Read P3 |
| - | //to set to input: pinMode(3,INPUT); | + | //To set to input: pinMode(3, INPUT); |
| - | //THIS IS P3, P3 is Analog Input 3, so when you are using analog read you refer to it as 3 | + | //THIS IS P3, P3 is analog input 3, so when you are using analog read, you refer to it as 3. |
| - | //sensorValue = analogRead(0); //read P5 | + | //sensorValue = analogRead(0); //Read P5 |
| - | //to set to input: pinMode(5,INPUT); | + | //To set to input: pinMode(5, INPUT); |
| - | //THIS IS P5, P5 is Analog Input 0, so when you are using analog read you refer to it as 0 | + | //THIS IS P5, P5 is analog input 0, so when you are using analog read, you refer to it as 0. |
| } | } | ||
| </code> | </code> | ||
| - | ===== Analog Write: (aka PWM)===== | + | ===== Analog Write: (AKA PWM)===== |
| <code> | <code> | ||
| void setup() { | void setup() { | ||
| - | //P0, P1, and P4 are capable of hardware PWM (analogWrite) | + | //P0, P1, and P4 are capable of hardware PWM (analogWrite). |
| - | pinMode(0, OUTPUT); //0 is P0, 1 is P1, 4 is P4 - unlike the analog inputs, for analog (PWM) outputs the pin number matches | + | pinMode(0, OUTPUT); //0 is P0, 1 is P1, 4 is P4 - unlike the analog inputs, |
| + | //for analog (PWM) outputs the pin number matches the port number. | ||
| } | } | ||
| void loop() { | void loop() { | ||
| - | analogWrite(0,255); //turn the pin on full (100%) | + | analogWrite(0,255); //Turn the pin on full (100%) |
| - | delay(1000); | + | delay(1000); |
| - | analogWrite(0,128); //turn the pin on half (50%) | + | analogWrite(0,128); //Turn the pin on half (50%) |
| - | delay(1000); | + | delay(1000); |
| - | analogWrite(0,0); //turn the pin off (0%) | + | analogWrite(0,0); //Turn the pin off (0%) |
| - | delay(1000); | + | delay(1000); |
| } | } | ||
| </code> | </code> | ||
| + | |||