W2-Digital I/O and Analog Input

Setting up Arduino on the breadboard

Finding a spot on the breadboard for Arduino

IMG_1638.jpeg

I realized that my longer breadboard is a bit different from the one illustrated in the lab note. The voltage bus and ground bus are flipped on two sides. And the column and row numbers are not marked slightly differently, but won’t influence the work, I think.

IMG_1637.jpeg

I noticed that all the ports numbers are indicated on the back of the board. So I had to compare or always count during the lab. I don’t know if it is the case for everyone, or maybe just because I got mine in china.

 

 Digital input and output

This is my first attempt setup. I found the buses are broke in the middle around row 30. So I connected them with jump wires. However, this didn’t make the board to work for some reason.

IMG_1639.jpeg

Then I moved the bottom two voltage and ground wires up, skipping the breadboard’s bottom half. Following the codes in the lab and uploaded to the Arduino, and it worked.

void setup() {
  pinMode(2, INPUT); //set the pushbutton pin to be an input
  pinMode(3, OUTPUT); //set the yellow (upper) LED pin to be an output
  pinMode(4, OUTPUT); //set the red (lower) LED pin to be an output
}

void loop() {
  //read the pushbutton input
  if (digitalRead(2) == HIGH){
    // if pushbutton is closed:
    digitalWrite(3, HIGH); // turn on the upper LED
    digitalWrite(4, LOW); //turn off the lower LED
  }
  else {
    //if the switch is open:
    digitalWrite(3, LOW); //turn off the upper LED
    digitalWrite(4, HIGH); // turn on the lower LED
  }

}
IMG_1642.jpeg
 

Analog Input

Connected a pot to the board and ran the following code. Worked as well.

const int ledPin = 9; //pin that led is connected to
int analogValue = 0; //value read from the pot
int brightness = 0; //PWM pin that LED is on

void setup() {
  //initialize serial communication at 9600 bps (bites per second)
  Serial.begin (9600);
  pinMode(ledPin, OUTPUT); //declare the led pin as an output
}

void loop() {
  analogValue = analogRead(A0); // read the pot value
  brightness = analogValue / 4; //divide by 4 to fit in a byte
  analogWrite(ledPin, brightness);
  Serial.println(brightness);
}
IMG_1644.jpeg

Then I replaced the pot with a phototransistor. Basically, the same connection replaced the ground wire with a 10k resistor and connected with the longer leg of the phototransistor. Running the same code, because all the ports and variables used are the same.

Here is the serial monitor when the phototransistor is connected. We can see the value changes as the brightness changes.

 

Force sensing resistor and finding range for map()

Next I tested with two force-sensing resistors and two LEDs.

IMG_1657.jpeg
const int leftLED = 9; //pin that the LED on the left is on
const int rightLED = 10; //pin that the LED on the right is on
int rightSensorValue = 0; 
int leftSensorValue = 0;

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

void loop() {
  rightSensorValue = analogRead(A1);
  int brightness = map(rightSensorValue, 400, 900, 0, 255);
  analogWrite(rightLED, brightness);
  Serial.println(rightSensorValue);

  leftSensorValue = analogRead(A0);
  brightness = map(leftSensorValue, 400, 900, 0, 255);
  analogWrite(leftLED, brightness);
  Serial.println(leftSensorValue);
}

I found that both LEDs are always on. So I think there should be something wrong with the code.

I guess the rang and the mapping are wrong. Since we are using different sensors.

I’m also seeing the LED dimmed when I pressed the sensor. It might be caused by the rightSensorValue is larger than the maximum value we set in the map(), so it counted back from 0. When a number larger than 255 is assigned to a byte variable, it will count from 0 again. So when the maximum is 900, and the value from the sensor is 902, it actually returns 1 ( This is just my guess, please let me know if I was correct or not)

I uploaded the following code, and going through serial monitor to find the lowest and hightest value from the sensor.

void loop() {
  rightSensorValue = analogRead(A1);

  Serial.println(rightSensorValue);
}

The lowest and the highest numbers I found are -204 and 987 )

So I had the following code:

void loop() {
  rightSensorValue = analogRead(A1);
  int brightness = map(rightSensorValue, -204, 987, 0, 255);
  analogWrite(rightLED, brightness);

  Serial.println(brightness);
  Serial.println(rightSensorValue);
  Serial.println("\t");
}

Then I found the lowest number shifted to 10, then back to -20.

So my LED wasn’t table. I’m guess I might have pressed to hard and damaged the sensor. But overall the brightness of the LED was more acurate to the pressure on the sensor.


Previous
Previous

W3-Analog Output & Project 1 Sketch

Next
Next

W1-The intro to intro to Prom