Categories
TUTORIALS

Changing RGB led display using a hall effect sensor

This tutorial will first introduce the hall effect sensor we used in our project and then cover how to control the RGB LED display using the output from the sensor. We design an intelligent reagent kit for addicted people to help them control the drug usage. What this tutorial will talk about is the magnet senior which is used to detect the action of opening the box as well as a responsive and warning LED, with which the box owner can easily know the status he/she is in, i.e, how many times they have opened the kit to get the drug within a specific time period.

Before we dig in, here is a parts list for you:

  1. Arduino Uno
  2. Jumper wires
  3. RGB LED
  4. 330 Ohm resistor * 3
  5. 10k Ohm resistor
  6. Hall effect sensor (magnet sensor)
  • Hall Effect Sensor

The hall effect sensor is used to measure the magnitude of a magnetic field. To be simple, it tells you whether there is a magnet around. If you want to know more about the Hall Effect, here is a link transferring you to Wikipedia!

Below is a diagram for the device. It has three pins. When a magnet is near the sensor, pin 3 will go up and the output will be HIGH. Thus, in our case, when the kit is close, the output should be HIGH and when someone open the kit to get the drug, the output will switch to LOW.

  • RGB LED

In our project, we have four status when detecting the action of the box owner to the kit in a period of time, and they are: the user has not opened the box yet (LED displays GREEN light), the user opens the kit (LED displays YELLOW light), the user closes the kit (LED displays RED light), and the user reopens the box without authorization (LED displays BLINKING RED light).

  • Combine together!!!

The magnet sensor is used to detect the opening and closing of the kit; the RGB LED is used as a responser to make our device user-friendly. The circuit diagram is shown below.

Make sure you connect the pin in the right way. We have a different look in the hall effect sensor used in the project and the one here in the diagram. Connect pin 1 to the power, pin 2 to the GND and then a 10K pull-up resistor from pin 3 to power.

The 4 pins of RGB LED is RED, COMMON, GREEN and BLUE in order. The COMMON pin is connected to the GND, and the other 3 pins are connected to a 330 Ohm resistor respectively.

  • Code it

As mentioned above, we define 4 status and to let the device know how it works, we have to translate into language it could understand as shown in the table below. If/else statement would be used here to control the change in different statuses.

OUTPUT of Hall Effect Sensor  Time(s) The User opens the kit LED Display
HIGH (Box closed)0GREEN
HIGH (Box closed)1RED
LOW (Box Open)1YELLOW
LOW (Box Open)>1BLINKING RED
int day = 0; // store the number of day since the user refills the kit
int count = 0; // store the times user opens the kit each day
int output = 0; // store the output status of magnet sensor
int pre_status = 0; // store the previous status
const int onSwitch = 2; // Define the pin for the magnet sensor 

// Define the pins for RGB LED
const int RED_PIN = 8;
const int GREEN_PIN = 9;
const int BLUE_PIN = 10;


void setup() {
  pinMode(onSwitch, INPUT);
  Serial.begin(9600);
}


void loop() {
  // Get the recorded time and format it into day, hour, minute and second
  long int current_time = millis()/1000;
  int current_day = current_time/(24*3600);
  int hour = (current_time/3600)%24;
  int minute = (current_time/60)%60;
  int second = current_time%60;

  // Print the time to the Serial Monitor
  Serial.print("Day:");Serial.print(current_day);
  Serial.print("  Time:");Serial.print(hour);
  Serial.print(":");Serial.print(minute);
  Serial.print(":");Serial.print(second);
  delay(1000);

  // Reset when the next time period (day here) comes
  if (current_day != day){
    day = current_day;
    count = 0;
  }

  // Change the LED display according to the output and the count

  // Read the output of the hall effect sensor
  int sensorVal = digitalRead(onSwitch);

  // Define two output status based on the sensor output
  if (sensorVal == HIGH){
    output = 0; // box close
  }
  else{
    output = 1; // box opened
  }
  
  // Use "if/else statement" to define 4 action status, as shown in table
  if (count <= 1){
    if (output == 0) {
      if(count == 0){
        showRGB(0);
        pre_status = 0;
        Serial.println("   User has not injected yet");
      }
      else{
        showRGB(2);
        pre_status = 2;
        Serial.print("   User has injected ");
        Serial.print(count);
        Serial.println(" time(s) today");
      }
    } 
    else{
      showRGB(1);
      Serial.println("   User opens the box");
      if (pre_status != 1){
        pre_status = 1;
        count = count + 1;
      }
    }
 }
 // When the count is greater than 1, which means the user probabaliy overdose,
 // the LED blinks to warn.
 else{
   digitalWrite(RED_PIN, HIGH);
   digitalWrite(GREEN_PIN, LOW);
   digitalWrite(BLUE_PIN, LOW);
   delay(300);
   digitalWrite(RED_PIN, LOW);
   delay(200);
   Serial.println("   Overdose");
  }
}


void showRGB(int LED_status)
{
  int redIntensity;
  int greenIntensity;
  int blueIntensity;

  // "if/else statement" is used here to define 4 display status.
  if (LED_status == 0)
  {
    // In status 0, the LED displays green light.
    redIntensity = 0;
    greenIntensity = 255;
    blueIntensity = 0;
  }
  else if (LED_status == 1)
  {
    // In status 1, the LED displays yellow light.
    redIntensity = 200;
    greenIntensity = 130;
    blueIntensity = 0;
  }
  else if (LED_status == 2)
  {
    // In status 2, the LED displays red light.
    redIntensity = 255;
    greenIntensity = 0;
    blueIntensity = 0;
  }

  // analogWrite() is used to set the brightness values of each LED and the
  // LED could generate different lights.
  analogWrite(RED_PIN, redIntensity);
  analogWrite(GREEN_PIN, greenIntensity);
  analogWrite(BLUE_PIN, blueIntensity);
}

Since we don’t have the video for you, we print the status into Serial Monitor instead to let you see how the status could be changed. In Serial Monitor, we obtain an output like this:

For reference, here is the table showing the LED light each status corresponding to.

StatusLED Light
User has not injected yetGreen
User opens the boxYellow
User has injected X time(s) todayRed
OverdoseBlinking Red

Ha! You make it. It is just so easy, isn’t it?! 😀

This project refers to the Mystery Box: Shutterglass Chamber on Adafruit.

Leave a Reply

Your email address will not be published. Required fields are marked *