Skip to content

7. Input & Output device

Week 7

Inputs sensors

Sensors are devices that perform a “Input” function because they “sense” a physical change in some characteristic that changes in response to some excitation, such as heat or force, and convert that into an electrical signal. I got the HW-505 A mercury switch module.

Mercury switches can be found in a wide range of consumer, commercial, and industrial products, such as appliances, space heaters, ovens, air handling units, security systems, leveling devices, and pumps.

How does it work?

Tilt sensors are switches that detect fundamental motion and orientation. When the metal tube is tilted upright, a small metal ball rolls around inside it and shorts the contacts sticking out of the end together.

Code

int led_pin = 13;
int switch_pin = 6; interface
int val;
void setup()
{
  pinMode(led_pin, OUTPUT);
  pinMode(switch_pin, INPUT);
}
void loop()
{
  val = digitalRead(switch_pin);
  if(val == HIGH)
  {
    digitalWrite(led_pin, HIGH);
  }
  else
  {
    digitalWrite(led_pin, LOW);
  }
}

OUTPUT Sensors

Actuators are devices that perform a “Output” function and are used to control some external device, such as movement or sound. I got the light cup sensor module.

The Magic Light Cup Module is a circuit board that contains an LED and a mercury tilt switch. When tilting the modules, the user can achieve the effect of light being “magically” transferred from one to the other by using PWM to drive the LEDs on the modules. In short, it consists of two components: a mercury tilt switch and a led.

Code

int brightness = 255;

void setup() {
  pinMode(6, INPUT);  
  pinMode(9, OUTPUT);
}

void loop() {
  if (digitalRead(6) == HIGH) {
    if (brightness < 255) brightness++;

  }
  else {
    if (brightness > 0) brightness--;

  }

  analogWrite(9, brightness);
}

How do I feel this week:

This week I experimented with new sensors that I had never used before; it was enjoyable, and as I previously stated, this topic is related to my major, so for sure it is one of my favorite weeks! Looking forward to the coming weeks.


Last update: August 16, 2022