Skip to content

Instantly share code, notes, and snippets.

@mepsrajput
Last active December 2, 2021 19:44
Show Gist options
  • Save mepsrajput/34b18c529c5d5c4c7d8194e1c01bface to your computer and use it in GitHub Desktop.
Save mepsrajput/34b18c529c5d5c4c7d8194e1c01bface to your computer and use it in GitHub Desktop.
Adruino Uno
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

Code Examples

Functions for setup()

pinMode()

Configures the specified pin to behave either as an input or an output.

  • Syntax: pinMode(pin, mode)
  • pin: the Arduino pin number to set the mode of.
  • mode: INPUT (to use the pin as input), OUTPUT (to use the pin as output) or INPUT_PULLUP (to use the built-in pullup resistors as input).

Functions for loop()

digitalWrite()

Write a HIGH or a LOW value to a digital pin.

  • If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW.
  • If the pin is configured as an INPUT, digitalWrite() will enable (HIGH) or disable (LOW) the internal pullup on the input pin.
  • Syntax: digitalWrite(pin, value)
  • pin: the Arduino pin number.
  • value: HIGH or LOW.

analogWrite()

Writes an analog value (PWM wave) to a pin.

  • Syntax: analogWrite(pin, value)
  • pin: the Arduino pin to write to. Allowed data types: int
  • value: the duty cycle: between 0 (always off) and 255 (always on). Allowed data types: int.

digitalRead()

Reads the value from a specified digital pin, either HIGH or LOW.

  • Syntax: digitalRead(pin)
  • pin: the Arduino pin number you want to read

analogRead()

Reads the value from the specified analog pin.

  • Syntax: analogRead(pin)
  • pin: the name of the analog input pin to read from (A0 to A5 on most boards).
  • Range: 0 to 1023

delay()

Pauses the program for the amount of time (in ms) specified as parameter.

  • Syntax: delay(ms)
  • pin: the Arduino pin number.
  • ms: the number of milliseconds to pause. Allowed data types: unsigned long.

Serial.begin()

Sets the data rate in bits per second (baud) for serial data transmission.

  • Syntax: Serial.begin(speed, config(optional)
  • Serial: serial port object.
  • speed: in bits per second (baud). Allowed data types: long.
  • config: sets data, parity, and stop bits.

Serial.begin()

Prints data to the serial port as human-readable ASCII text followed by a carriage return character (ASCII 13, or '\r') and a newline character (ASCII 10, or '\n').

  • Syntax: Serial.println(val, optional)
  • Serial: serial port object.
  • val: the value to print.
  • format: specifies the number base (for integral data types) or number of decimal places (for floating point types).

Analog: 0 to 255 Volts: 0 to 5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment