Magic Cup Module with Arduino

magic-cup-module

This is rather an odd module, I had seen them, but only purchased one recently. I assume they are designed to sit at the bottom of a cup and the LED lights up when the cup is tilted. There are four connectors marked +, -, S and L on my board. I decided to draw the circuit out to see how it would connect, the circuit diagram is shown below.

magic-cup-diagram

At first sight it seemed a little odd, I connected pin S and L together and connected the + to the Arduino 5 volt pin and the – to the Arduino Gnd pin. Tilt the module and the LED lights, tilt it back and the LED goes off, the LED is lit when the Mercury switch contact is BROKEN. I assume the reason is that the current drawn through the LED does not pass through the Mercury tilt switch. When the Mercury switch is closed the LED is shorted and a small current flows through the resistor and switch to Gnd. When the Mercury Switch is open the current flows through the resistor and LED to Gnd and it lights up.

magic-cup1

This picture shows the mercury bead does not cover both contacts and the switch is OPEN.

magic-cup2

This shows the contacts CLOSED as the Mercury covers both contacts and the LED is OFF.

The circuit does not really need the Arduino and will work happily with juts a 5 volt power supply. However, as both the switch and LED are available individually I wrote a short Sketch that allows the operation to be changed, or used in a much bigger project.

/**************************************************
*
* Magic Cup Demonstration
*
* Chris Rouse December 2016
*
* Connections:
* + or Vcc to Arduino 5 volts
* – to Arduino Gnd
* S (Mercury Tilt Switch) to Arduino pin 8
* L (LED connection) to Arduino pin 7 via a 220R resistor
*
* Note: a 220R resistor must be used in series with the LED or it will be destroyed
*
**************************************************/

#define mercuryTilt 8
#define LED 7
#define onBoardLed 13
boolean state = false;
boolean initialState;

void setup() {
pinMode(mercuryTilt, INPUT);
pinMode(LED,OUTPUT);
pinMode(onBoardLed, OUTPUT);
initialState = digitalRead(mercuryTilt);
digitalWrite(LED, LOW); // start with LED off
}

void loop() {
state = digitalRead(mercuryTilt);
if(state != initialState){
digitalWrite(LED, HIGH); // turn LED ON if switch has been tilted
digitalWrite(onBoardLed, HIGH); // echo output to Arduino’s LED
}
else{
digitalWrite(LED, LOW);
digitalWrite(onBoardLed, LOW);
}
}

The sketch first determines which way up the tilt switch is and makes this the OFF position, when the switch is tilted the LED will be turned ON. to reverse this action change the line

if(state != initialState){

to

if(state == initialState){

The LED will be ON until the switch is tilted

I admit that this circuit could have been built using discreet components, but I purchased two of these modules for less than £1 on eBay, post paid.

 

Advertisements