due at the start of Class 5: Tuesday, Sept. 11th

1. A reasonable nightlight

  • Inputs
    • a switch
    • a potentiometer
    • a photoresistor (light sensor)
  • Outputs
    • an LED
    • Serial monitor feedback

Nightlight operation: when it gets darker in the room, the LED should turn on. When it gets brighter, it should turn off.

Turning the potentiometer adjusts the “setpoint,” i.e. the threshold at which the light turns on/off.

Reverser switch: there is a mode switch which can change the nightlight into a daylight. That is to say, when that switch is in “day” mode, it turns on when it’s bright out, and off when it’s dark out. (The switch can be flipped to “day” or “night” mode whenever the user wants.) The potentiometer still adjusts the setpoint just as it did before.

Serial feedback: in order to help understand what the Arduino is seeing, and how bright the room is, add some Serial feedback in this format:

room brightness: 320; setpoint is: 300; switch is: NIGHTLIGHT; LED is: OFF
room brightness: 100; setpoint is: 300; switch is: NIGHTLIGHT; LED is: ON
room brightness: 100; setpoint is: 300; switch is: DAYLIGHT; LED is: OFF

Recommended general plan of attack for this assignment: add only one component at a time. Maybe start with only the photosensor, make sure it’s working (the Serial feedback will help a lot with that), add the switch, make sure it’s working, etc. Build towards the goal, and stop at ~4 hours (or keep going if you want!). If you complete the assignment, great, and if not, get as far as you can in ~4 hours. We’re at the learning-a-lot-very-quickly stage of the course, which is coincidentally also the this-is-too-much-at-once stage.

Code formatting expectations

The top of your code should have an introductory multi-line comment block like this:

 * Project or Homework Name
 * by Student Name (andrewid)
 * date
 * Brief explanation of what the below code does

You should use single-line comments to explain anything not obvious. These aren’t only notes for other people—they’re notes for you when you look at the code next month and are trying to figure it out!

A best practice is to write constant variable names in ALL_CAPS and changeable variable names in camelCase.

Deliverables for Part 1:

  • Working nightlight, as described above, ready to present at the start of class
  • Working serial feedback, as described above
  • Electrical schematic of the circuit you built, with your name and date at the top, to be handed in
  • Only to be submitted if you do not go on to do Part 2 below: Code to be submitted via email by the start of class. File naming convention: andrewid-hw4.ino. Email to RZ with the file name as the subject line.

Some code hints to help you get started if you’re not sure where to begin

First, begin by gathering data about the world. In the setup(), use the pinMode() commands to tell the Arduino where everything is plugged in, and what things are inputs and what things are outputs. Also, in the setup(), don’t forget to add the command to begin Serial communication.

Then, in the loop(), begin by gathering information like this:

int brightnessVal = analogRead(A0); // the pin the photoresistor is plugged into
int potVal = analogRead(A1);        // the pin the potentiometer is plugged into
int switchVal = digitalRead(7);     // the pin the switch is plugged into

Now you have to write the instructions that will turn the LED on or off. It’s a simple question that the code needs to run: is the brightness value lower than the potentiometer value? If so, turn on the LED. If not, turn off the LED.

if (brightnessVal < potVal){
    // turn on the LED in here

This will set you on your way, but there’s things I left out. For instance, I haven’t told you how to add the logic to make it so that the switch reverses the behavior of the LED to make it into a daylight. The exercise is left to the reader!

Remember to use the built-in reference system in the Arduino software if you’re confused about how to use any functions.

2. A nightlight with feature bloat

Do this part of the assignment only if you were able to successfully do #1 first!

  • Inputs are same as in #1 above, plus:
    • at least one button
  • Outputs are same as in #1 above, plus:
    • a servomotor

In addition to being able to function just like the reasonable nightlight in #1, you must add at least one button that does something, and a servomotor that does something else. What do they do? I don’t know. You are at liberty to decide for yourself. It should be something silly, funny, or even (if you can think of it) useful. It should not be as simple as “when I push the button, the motor moves to this position,” because that’s quite boring.

Write a very short user story describing the thinking behind your nightlight-plus. For instance: “After the sun sets, Jill falls asleep. Then, in the morning, when the sun rises, she doesn’t wake up fast enough, so her nightlight slaps her face until she pushes this button.”

Inspiration for those feeling uninspired: * When a nightlight sees the transition from dark to light, that means it’s dawn. What could it do (using the motor) at dawn? Likewise with dusk. * Does something strange happen when you hold the button down? It changes the nightlight’s behavior temporarily? How? * Could the Serial messages say something different, based on what’s happening?

Deliverables for Part 2:

  • Code to be submitted via email by the start of class. File naming convention: andrewid-hw4.ino. Email to RZ with the file name as the subject line.

3. Coordinate with your partner to begin working on Project 1.

You are free to do this as you like, but at the least, each partner should independently sketch out 5 simple doodles of ideas they have for Project 1: Surprise!, with a sentence or two of explanation, and share them with the other. This first project is as much about ideation as it is about the technical build and settling on an interesting idea early on will give you time to build an iteration or two and change your path along the way. So don’t put it off!

Collaboration and help

Limited collaboration with your colleagues is permitted. You must build your own circuit and write your own code, but you can talk with other students to get advice and reason through the problem together. You are not permitted to copy somebody else’s code. You are permitted to use any internet/book/etc. resource you’d like.


The homeworks prior to this had simple complete/incomplete grading. For this assignment, the grading is a bit more granular:

points assignment
3 Part 1: all inputs wired properly
1 Part 1: output wired properly
2 Part 1: serial feedback formatted properly and working
4 Part 1: code written cleanly, clearly, and with appropriate commenting practice
5 Part 1: device functions as intended
2 Part 2: additional inputs/outputs wired properly
3 Part 2: device functions as described by student
20 total