Lab 1
Microcontrollers
Read the Report


Lab 1


Learn basic functionalities of the Arduino Uno, the Arduino IDE, and the GitHub repository. In particular, focus on writing a program to control multiple external components connected to the Arduino Uno. At the end of the lab, put together a robot and have it perform a simple autonomous task.

To begin, we split into two groups of two. Each group progressed through the lab individually, as described below.


GOAL: use the Blink sketch to control an internal LED

All Arduino sketches require two functions:

    void setup() {}
    void loop() {}

Our example sketch code:


      void setup(){
          // initialize digital pin LED_BUILTIN as an output.
          pinMode(LED_BUILTIN, OUTPUT);
      }
      

    // the loop function runs over and over again forever
    void loop() {
      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
      delay(1000);                       // wait for a second
      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
      delay(1000);                       // wait for a second
    }
      

GOAL: modify the Blink sketch to control an external LED



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

      void loop() {
        digitalWrite(0, HIGH);   // turn the LED on (HIGH is the voltage level)
        delay(1000);                 // wait for a second
        digitalWrite(0, LOW);    // turn the LED off by making the voltage LOW
        delay(1000);                  // wait for a second
      }
      

Green LED connected to digital pin 0


GOAL: read an analog value and verify it is correct via serial monitor


      int readValue = 0;
      int pinName = A0;
      

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

      void loop() {
        readValue = analogRead(pinName);
        Serial.println(readValue);
        delay(500);
      }
      

Analog values printed to the serial monitor

The circuit


GOAL: use a potentiometer to control the speed of a servo


      int analogInput = A0;
      int digitalPin = 11;
      int voltageValue = 0;
      int delayTime = 500; //half a second

      void setup() {
        Serial.begin(9600);
        pinMode(digitalPin, OUTPUT);
      }

      void loop() {
        voltageValue = analogRead(analogInput);
        Serial.println(voltageValue); //Prints to serial monitor, used for debugging purposes
        delay(delayTime);
        analogWrite(digitalPin, voltageValue/4);
      }
      

Demo:


GOAL: use a potentiometer to control the brightness of an LED


        #include 
        int val = 90;
        int readValue = 0;
        int pinName = A0;
        Servo myservo;

        void setup() {
          myservo.attach(3);
        }

        void loop() {
          readValue = analogRead(pinName);
          val = map(readValue, 0, 670, 0, 180);
          myservo.write(val);
        }
      

Demo:


GOAL: assemble the robot and make it perform a short autonomous task




For our code, we wrote a header file with the following movement commands:


        // Stops the robot
        void stopMotors(Servo servo_L, Servo servo_R);

        // Drives the robot forward
        void moveForward(Servo servo_L, Servo servo_R);

        // Drives the robot backward
        void moveBackward(Servo servo_L, Servo servo_R);

        // Turns the robot right slightly, based on [amount]
        void adjustRight(Servo servo_L, Servo servo_R, int amount);

        // Turns the robot left slightly, based on [amount]
        void adjustLeft(Servo servo_L, Servo servo_R, int amount);

        // Turns the robot 90 degrees right, in place
        void turnRight(Servo servo_L, Servo servo_R);

        // Turns the robot 90 degrees left, in place
        void turnLeft(Servo servo_L, Servo servo_R);
        

By running these commands sequentially with delays, we can program our robot to move around in arbitrary patterns. We used these functions to move our robot around a square in the video.