Lab 5 - gracie-miller/BAE305-S19 GitHub Wiki

Lab 5 - Microcontrollers

By: Gracie Miller and Rachel Rohrer

Summary

The goal of this lab was to become familiar with using Arduino and how the software controls hardware. Four built in example Arduino programs were used and modified: Blink, AnalogReadSerial, DigitalReadSerial, and AnalogInOutSerial.

Materials

  • Breadboard
  • Wires
  • LED
  • 220 Ω Resistor
  • 10 kΩ Resistor
  • Button
  • Potentiometer

Assembly Procedures

There were five different circuits built for the four Arduino built in examples.

Blink:

The Blink example circuit was built following this schematic:

Blink. (2015, July 28). Retrieved from: https://www.arduino.cc/en/Tutorial/Blink

The circuit looked like this:

After the circuit was built, it was connected to the laptop. The Blink built in example was opened and run. The Blink code did not require any alterations in order to perform correctly with the circuit that was built. The code is shown below:

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

void loop() {
  digitalWrite(13, HIGH);
  delay(1000);
  digitalWrite(13, LOW);
  delay(1000);
}

AnalogReadSerial:

The AnalogReadSerial example circuit was built following this schematic:

Analog Read Serial. (2015, July 28). Retrieved from: https://www.arduino.cc/en/Tutorial/AnalogReadSerial

A second potentiometer was added to the circuit instead of just one, as is in the schematic, and connected to a second analog pin. The circuit looked like this:

After the AnalogReadSerial circuit was built, it was connected to the laptop. The AnalogReadSerial built in to fit the new circuit. The modified AnalogReadSerial code is shown below:

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

void loop() {
  float sensorValue = analogRead(A0)* (5.0/1024.0);
  float sensorValue1 = analogRead(A1)* (5.0/1024.0);
  // print out the value you read:
  Serial.print(sensorValue);
  Serial.println(" V");
  Serial.print(sensorValue1);
    Serial.println(" V");
  delay(1000);
}

DigitalReadSerial:

The DigitalReadSerial example circuit was built following this schematic:

Digital Read Serial. (2015, July 28). Retrieved from: https://www.arduino.cc/en/Tutorial/DigitalReadSerial

The circuit looked like this:

The code attached to this circuit goes as follows:

int pushButton = 2;

void setup() {
  Serial.begin(9600);
  pinMode(pushButton, INPUT);
}

void loop() {
  int buttonState = digitalRead(pushButton);
  Serial.println(buttonState);
  delay(1);
}

Modified:

In the same circuit as above, the resistor was replaced with an LED The circuit looked like this:

After the DigitalReadSerial circuit was built, it was connected to the laptop. First, the DigitalReadSerial built in example was opened and run while operating the switch. Later, when the LED was added, the code was altered such that when the switch was pushed down the LED turned on, and when the switch was not pushed down, the LED was off. The modified DigitalReadSerial code is shown below:

int pushButton = 2;
int LED = 9;

void setup() {
  Serial.begin(9600);
  pinMode(pushButton, INPUT);
  pinMode(LED, OUTPUT);
}

void loop() {
  int buttonState = digitalRead(pushButton);
  Serial.println(buttonState);
  if (buttonState == HIGH) {
    digitalWrite(LED, HIGH);
  }
  else {
    digitalWrite(LED, LOW);
  }
}

AnalogInOutSerial:

The AnalogInOutSerial example circuit was built following this schematic:

Analog In, Out Serial. (2015, July 28). Retrieved from: https://www.arduino.cc/en/Tutorial/AnalogInOutSerial

The circuit looked like this:

After the AnalogInOutSerial circuit was built, it was connected to the laptop. The AnalogInOutSerial built in example was opened and run. The AnalogInOutSerial code is shown below:

const int analogInPin = A0;
const int analogOutPin = 9;

int sensorValue = 0;
int outputValue = 0;

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

void loop() {
  sensorValue = analogRead(analogInPin);
  outputValue = map(sensorValue, 0, 1023, 0, 255);
  analogWrite(analogOutPin, outputValue);

  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\t output = ");
  Serial.println(outputValue);

}

Test Equipment

  • Laptop Computer
  • Arduino

Test Procedures

Because this lab serviced as a way familiarize ourselves with using Arduino and its components, there were very few test procedures to be conducted. For each of the circuits built, the accompanying code was run in order to ensure that both the circuit was set up and that the code was written correctly.

Discussion

The code for each of circuits built was composed of declarations, setup, and the loop. The declarations initialize the input and output pins and gives names to specific pins. The setup section of the code initializes communication rates and can also name specific pins. The loop runs a continuous set of specified functions for forever. Each of these sections of code are modified to fit both a specific project and circuit.

The Blink program operated a simple circuit of just the LED and resistor in series. The circuit was connected to pin13 on the Arduino, so in the code, pin13 was set as the output. This was done in the void setup section so that it only had to be done once. In the void loop portion of the code,the digitalWrite commands alternated between passing high voltage (~5V) and low voltage (~0V) through the circuit, holding each setting for one second. The LED was on during periods of high voltage and off otherwise.

The AnalogReadSerial program was used to read the resistance of a potentiometer. The float commands converted the 0-1024 analog scale of the potentiometers to a voltage reading between 0 and 5V. It also established a variable to represent the two potentiometers and which of the two analog pins each was connected to. Serial.print would display the readings from the potentiometers onto a scrolling window.

The DigitalReadSerial program was used as it is in the example program list and also with a small modification. The int function preceeding void setup is just establishing that the button is connected to digital pin2. The int function within void loop assigns the variable buttonState to be the position of the button when read from the pin. Finally, Serial.print displays the state of the button, a "1" if pressed and a "0" if not. The modified version replaces the display to show the state of the button with an LED. An if else statement was used. The if statement followed by the digitalWrite sends 5V through the circuit when the button is pressed. The else statement sends 0V through the circuit when the if statement is not satisfied, so when the button is not pressed.

The AnalogInOutSerial program read the input pin of the potentiometer and controlled the brightness of the LED in the circuit based on the voltage passing through the potentiometer. Additionally, the Serial.print function displayed the voltages of both the potentiometer and the LED.

One issue that we encountered during this lab was the organization of the circuit. We built all of the circuits without deconstructing any of the circuits, so we had multiple circuits running off the Arduino at the same time. This got to be a little confusing towards the end of the lab. If we were to repeat this lab, we would have built deconstructed each circuit before assembling the next one.