Arduino Tutorial

By LiterallyTheOne

5: Analog

Arduino Tutorial, Analog

Introduction

  • Previous tutorial: Serial Communication
  • This tutorial: Analog
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

Analog Read

  • digitalRead
  • analogRead (10bit)
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

Analog pins

  • 6 pins: A0 to A5
  • Showed by yellow
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

Potentiomenter

  • Creates voltage between and
  • In SimulIDE: Passive/Resistors/Potentiometer
  • pins and a rotating button
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

Connect potentiometer

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

Potentiomenter and Arduino

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

Write the code for analogRead

#include <Arduino.h>

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  int our_input = analogRead(A0);
  Serial.println(String(our_input));
  delay(1000);
}
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

Result of analogRead

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

PWM

  • Pulse Width Modulation
  • Technique for controlling the power
  • Arduino uses it for writing analog values
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

PWM duty cycle

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

Analog Write pins

  • Uses PWM
  • There are 6 pins: 3, 5, 6, 9, 10, 11
  • In arduino: ~
  • In SimulIDE: PWM
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

Analog Write

  • digitalWrite:
  • analogWrite:
    • duty cycle
    • duty cycle
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

Conect LED

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

Write code for analog write

#include <Arduino.h>

void setup()
{
  pinMode(3, OUTPUT);
}

void loop()
{
  for (int i = 0; i < 256; i++)
  {
    analogWrite(3, i);
    delay(10);
  }
}
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

Result of analogWrite

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

See PWM with Oscope

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

Combining both

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

Write the code for combining both

#include <Arduino.h>

void setup()
{
  pinMode(3, OUTPUT);
}

void loop()
{
  int our_input = analogRead(A0);
  int brightness = map(our_input, 0, 1023, 0, 255);
  analogWrite(3, brightness);
  delay(1000);
}
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

map function

  • maps our input to the range that we want
  • first argument: input
  • second and third: input's range
  • forth and fifth: output's range
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

Result of combining both

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

Result of combining both with Oscope

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

DC Motor

  • Converts electrical energy into rotation
  • Examples:
    • Drill
    • Saw
    • Toys
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

mosfet

  • DC motor takes to much current
  • Should not be connected directly to Arduino pin
  • We use mosfet to write analog data
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

Connect DC Motor to Arduino

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

Result of DC motor

  • Code is the same

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

Servo motor

  • Precise control
  • Robotics
  • Position motor with PWM
  • Simple DC Servo motor
  • [-90, 90] ([0, 180])
  • frequency:
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

Connect Servo motor to Arduino

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

Servo library

  • Generates frequency
  • in PlatformIO:
lib_deps =
    ...
    arduino-libraries/Servo
  • in main.cpp:
#include <Servo.h>
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

Servo functions

  • Create an object
Servo my_servo;
  • identify the pin
my_servo.attach(3);
  • Write angle
my_servo.write(45);
By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

Result of Servo motor

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

Result of Servo motor with Oscope

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

Combine All

By Ramin Zarebidoky (LiterallyTheOne)
Arduino Tutorial, Analog

By Ramin Zarebidoky (LiterallyTheOne)