Setup Pin Mode

  pinMode(13, OUTPUT);     
  pinMode(12, INPUT);
  pinMode(12, INPUT_PULLUP);    //Input with pullup resistor

Set Outputs

  digitalWrite(13, HIGH);
  digitalWrite(13, LOW);

  digitalWrite(13, 1);
  digitalWrite(13, 0);

Read Inputs

Using a variable:

  int MyInputState;
  
  MyInputState = digitalRead(3);
  if (MyInputState == HIGH)
    DoSomething();

Using directly:

  if (digitalRead(3))
    DoSomething();

Analog Pins As General IO

The analog pins can be used identically to the digital pins, using the aliases A0, A1, etc.

  pinMode(A0, OUTPUT);
  digitalWrite(A0, HIGH);

Device Pin Numbers

You should always use Arduino pin numbers (e.g. 1, 2, 3, A0, A1, A2) and not device port pin numbers (e.g. PA0, PA1, PB0, PB1).

Whilst you may get things to work using port numbers, it can trip you up (e.g. ATtiny841 PA pin numbers working but PB pin numbers not)

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *