5.1 Lab: Sensors and Actuators

In the following Lab we will use two arduinos. One of them will be connected to a sensor and the other one to an actuator. We will connect both arduinos wirelessly using the XBees and XBee Explorer breackout boards. This way, an action on a sensor can trigger a reaction in an actuator somewhere else.

In the example we use a push-button as a sensor and a noisy buzzer as an actuator. Feel free to use any sensor and actuator that you like.



int BUTTON = 2;
void setup() {
    pinMode(BUTTON, INPUT);
    Serial.begin(9600);
}
void loop() {
    // send a capital D over the serial port if the button is pressed
    if (digitalRead(BUTTON) == HIGH) {
        Serial.print('D');
        delay(10); // prevents overwhelming the serial port
    }
}

int BELL = 5;
void setup() {
    pinMode(BELL, OUTPUT);
    Serial.begin(9600);
}

void loop() {
// look for a capital D over the serial port and ring the bell if found
    if (Serial.available() > 0) {
        if (Serial.read() == 'D'){
        //ring the bell briefly
        analogWrite(BELL, 180); //ranging from 0-255
        delay(10);
        analogWrite(BELL, 0);
        }   
    }
}

Activity:

After building your first sensor and actuator network, it is time to get into more advancing prototypes. One option is to include a feedback loop. For example the arduino controlling the buzzer can send a message to the arduino monitoring the sensor to confirm that the buzzer is actually buzzing. When the arduino connected to the push-button receives the confirmation, it lights up an LED so that the person pushing the button knows that the buzzer is buzzing. If you want to make it even more interesting join forces with some friends and build a network with multiple sensors and multiple push-buttons. Each of the push buttons can trigger a different sound.

View Projects 2014 Edition


Comments

comments powered by Disqus