User Tools

Site Tools


digispark:tutorials:basics

This is an old revision of the document!


Digispark Basics

DigitalWrite:

void setup() {
  //All pins are capable of Digital output, though P5 is 3v at HIGH instead of 5v
  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() {
	digitalWrite(0,HIGH); //turn the pin HIGH (5v)
	delay(1000);
	digitalWrite(0,HIGH); //turn the pin LOW (GND)
	delay(1000);
}

Digital Read:

int sensorValue = 0;

void setup() {
  //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
}

void loop() {
	sensorValue = digitalRead(1); //returns HIGH or LOW (true or false /  1 or 0)
}

Analog Read:

int sensorValue = 0;

void setup() {
  // The analog pins are referenced by their analog port number, not their pin number and are as follows:
  pinMode(1, INPUT); //THIS IS P2, P2 is Analog Input 1, so when you are using analog read you refer to it as 1
  //pinMode(2, INPUT); //THIS IS P4, P2 is Analog Input 2, so when you are using analog read you refer to it as 2
  //pinMode(3, INPUT); //THIS IS P3, P3 is Analog Input 3, so when you are using analog read you refer to it as 3
  //pinMode(0, INPUT); //THIS IS P5, P5 is Analog Input 0, so when you are using analog read you refer to it as 0
}

void loop() {
	sensorValue = analogRead(1); //read P2
	//sensorValue = analogRead(2); //read P4
	//sensorValue = analogRead(3); //read P3
	//sensorValue = analogRead(0); //read P5
}

Analog Write: (aka PWM)

void setup() {
  //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
}

void loop() {
	analogWrite(0,255); //turn the pin on full (100%)
	delay(1000);
	analogWrite(0,128); //turn the pin on half (50%)
	delay(1000);
	analogWrite(0,0); //turn the pin off (0%)
	delay(1000);
}
digispark/tutorials/basics.1357206808.txt.gz · Last modified: 2013/01/03 01:53 by digistump