Pcomp Lab: Digital Input and Output with an Arduino

IMG_1452

This is my first attempt to do a practical arduino lab.  In this lab we will be connecting a digital input (switch) & a digital output circuit to a microcontroller. We’ll control 2 leds (the red and green lights) using a switch (the small black button near the center of the breadboard).

As I assembled the circuit, I found that the main granular choice I had to make was which resistors to use.  I experimented with a few different resistors for which I couldn’t really see much difference in LED brightness.  This resistor-strength thing is a bit over my head at the moment compared to most of the other elements of understanding this stuff— I will kind of let it go for now until I get a better hang of it, probably by practicing some fundamental applications via labs…  Anyway, once my board was built, I plugged it into my laptop via USB and opened up the Arduino code editor….

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

I copy and paste the code from the lab into the appropriate places– I take a look at it to understand it– The digitalWrite() function is key here— we put it in the correct if/else statements to make it so if the switch isPressed, RED lights up.  ELSE, GREEN lights up.  So the “digitalWrite(4, HIGH)” in the “else” says that when the switch isn’t pressed, Green is lit up.  “else” is “all times other than when the switch is pressed.”  If digitalRead(2) == HIGH, that means the SWITCH (plugged into terminal 2) is being PRESSED.  It’s quite intuitive, really.  If the switch is being pressed, then digitalWrite(3, HIGH) because the Red light is plugged into terminal 3.

Anyway, learned a lot from this lab.  Lookin forward to getting more creative as I get more comfortable with the components.

Categories: itp, lab, pcomp

Tagged as: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s