loading

Show info
Amy Roberts

Amy Roberts is a product designer currently based in Austin, TX.

Published March 28, 2016

Arduino: Hello World

Light Sensor (Analog Input)

Description

I connected a photo cell light sensor (CdS photoresistor) to my Arduino and used the serial monitor to continuously print out light levels every 5 seconds.

Process

I began by going through the light sensor tutorial on adafruit.com. Although I didn’t completely understand some of the concepts, I was able to use an example piece of code and diagram to create a working build on my Arduino for detecting and printing levels of light. I changed the delay for printing to the serial monitor to 5 seconds.

IMG_6295-editIMG_6291-editIMG_6292

Reflections

Because the Arduino diagram on the site was in a different orientation than mine, I found it challenging at first to figure out which pieces to connect where. When I did figure it out, I had to use longer wires in some spots.

I was surprised by how easy this sketch was to implement and how responsive the light sensor is. I was aware that light sensors are an important element in smart phones today, but I had no idea they were so small and inexpensive. The light sensor seems like it will be useful in many different contexts.

Code

/* Photocell simple testing sketch.
Connect one end of the photocell to 5V, the other end to Analog 0.
Then connect one end of a 10K resistor from Analog 0 to ground
For more information see http://learn.adafruit.com/photocells */
int photocellPin = 0; // the cell and 10K pulldown are connected to a0
int photocellReading; // the analog reading from the analog resistor divider
void setup(void) {
// We’ll send debugging information via the Serial monitor
Serial.begin(9600);
}
void loop(void) {
photocellReading = analogRead(photocellPin);
Serial.print(“Analog reading = “);
Serial.print(photocellReading); // the raw analog reading
// We’ll have a few threshholds, qualitatively determined
if (photocellReading < 10) {
Serial.println(“ - Dark”);
} else if (photocellReading < 200) {
Serial.println(“ - Dim”);
} else if (photocellReading < 500) {
Serial.println(“ - Light”);
} else if (photocellReading < 800) {
Serial.println(“ - Bright”);
} else {
Serial.println(“ - Very bright”);
}
delay(5000);
}

Source: https://learn.adafruit.com/photocells/using-a-photocell

Serial Monitor Output

While the code was running, an analog reading of the detected light levels was displayed in the serial monitor every 5 seconds. I cupped my hand over the light sensor to change the light levels.

Screenshot 2016-02-20 20.46.05

Magnetic Sensor (Digital Input)

Description

I connected the Hall Effect sensor (magnetic detector) to my Arduino and continuously printed the presence or absence of a detected magnetic field.

Process

I followed along in class for this activity. To create my build, I used code and an Arduino diagram from a page containing tutorials for a bike computer. I used a magnet that came in my Arduino kit to test the sensor.

IMG_6298-editIMG_6283-editIMG_6285

Reflection

This build was a little more confusing for me because it involved several small pieces close together. The diagram I used was a different orientation than my Arduino, so I had to move things around a bit and adjust wire lengths. Working in class alongside other people made it easier because we could help each other out.

At first, I thought that my code or wiring was wrong because the sensor was not detecting the magnet. Then I realized that I just had to hold the magnet very close to the sensor. After working on the light sensor build, I was surprised to see just 1s and 0s printed in the serial monitor instead of a range, but that is the nature of digital input.

Code

// MagTest

// just blinks an LED whenever the magnetic sensor reads that the magnet is in range
// also uses the serial monitor window to continuously display the value of the magnetic sensor

const int hallPin = 2;
const int ledPin = 13;

const int magnetIn = LOW;  // the sensor returns LOW when the magnet is present
const int magnetOut = HIGH;

void setup () {
  pinMode (hallPin, INPUT);
  pinMode (ledPin, OUTPUT);
  Serial.begin (9600);
  Serial.println (“*** start”);
}

void loop () {
  int mag = digitalRead(hallPin);
  Serial.println(mag);

  if (mag == magnetIn) {
    digitalWrite(ledPin, HIGH);
  } 
  else {
    digitalWrite(ledPin, LOW); 
  }
}

Source: Bike Computer Examples, Magnet – http://blogs.uw.edu/hcdepcom/assignments/bike-computer

Serial Monitor Output

Screenshot 2016-02-19 11.09.51

LEDs (Digital Output)

Description

I created a circuit by connecting 3 LEDs independently to my Arduino. For each one, I used a current-limiting resistor of 330Ω. I then found and modified code to flash the LEDs in a sequence of red and green light. To output what was happening at each step, I used the serial monitor.

Process

I began by searching through tutorials and examples of code to find something to build off of. I found the code to create my sketch on an instructables.com post and used this along with photos posted by a previous student of the breadboard wiring to create my build. I then added some code to print to the serial monitor.

IMG_6297-editIMG_6264IMG_6267

Reflection

It was challenging for me to find a simple enough example piece of code from a reliable source. Much of the code I came across in my search for examples involved more complexity than I needed. I finally found a basic piece of code, but it did not include serial monitor printing. Since I have only worked with printing sensor data to the serial monitor, I wasn’t sure how to go about printing data for different LEDs, so I manually coded it to print after each light turns on.

I also wanted to make two of the lights turn on at once, but after some research, I learned that true multitasking is not possible to do with the Arduino UNO. I hope to explore some of the workarounds in future projects.

Code

int led = 13;
int led2 = 12;
int led3 = 11;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
Serial.println(“LED 1”);
delay(100); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(100);
{digitalWrite(led2, HIGH);
Serial.println(“LED 2”);
delay(100);
digitalWrite(led2, LOW);
delay(100);}
{digitalWrite(led3, HIGH);
Serial.println(“LED 3”);
delay(100);
digitalWrite(led3, LOW);
delay(100);}// wait for a second
}

Serial Monitor Output

When I ran the code, the serial monitor output the current lit LED bulb.

Screenshot 2016-02-20 17.28.35

amy