ESP8266 Magic 8 Ball

Do you have the urge to have Magic 8 Ball, but never get around to get one? Well I do. So what can I do in this case? I make one using ESP8266. This is the document on what I have done and what can be further improved.

Step 1. Gather all the Materials

Please note that some of the link above might be an affiliate link.

Step 2. The Circuit

The idea is to have a circuit that is activated when it is shaken. So we achieve this by using the shake sensor connected to pin D5 and GND. When the circuit is shaken it will get activated and we randomly answer the questions and display the results in the Display. At the same time we activated the LED to show that we are processing the input.

Step 3. Putting it together (connecting the charge module to the battery)

I soldered the charging module directly to the battery and then connected to the rest of the circuit using the prototype board as shown in the following picture.

Step 4. Combine the rest of the circuit

Connect the rest of the circuit by wiring up the shake sensor to D5 and GND, connect the LED to pin D6 and VCC and last but not the least, the OLED display SCL to pin D4 and SDA to pin D2. Once this is done connect the circuit to the battery via the VIN pin and GND.

Step 5. Load the Code and testing

Once you have the complete circuit, it is time to load the code and test the circuit. The code is using the SSD1306 library, OneButton library as describe in the previous post to detect the double click action when the ball is being shaken.

The code will detect the double click from the Onebutton library, and it will execute the following function:

// this function will be called when the button was pressed 2 times in a short timeframe.
 void myDoubleClickFunction(){
     started = false;
     digitalWrite(LEDpin, LOW);
      int answer = random(8);
     Serial.print("Double click:");
     Serial.println(answer);
     display.clear();
     display.setTextAlignment(TEXT_ALIGN_LEFT);
     display.drawString(0, 10, "Thinking …..");
     display.display();
     delay(1000);
     display.clear();
     display.drawString(0, 24, answers);
     display.display();
     delay(2000);
   started = !started;
   display.clear();
   display.display();
   digitalWrite(LEDpin, HIGH);
 }

As you can see the code above will turn on the LED by pulling it low. Then generate a random number between 0 – 7 and it will write the word “Thinking ….” for 1 second before displaying the answer for another 2 seconds. It will then clear the display and turn the LED off.

The full source code can be found in the following link. If all goes well you should be able to see the result like the following video.

Step 6. After thoughts

Now that you have an internet connected Magic 8 ball, you can expand it using your imagination. Here are a few examples:

  • Expand it to post the answers via MQTT
  • Trigger the magic 8 ball from the internet (this will freak your friend out)
  • Expand to have more than 8 answers
  • The possibility is only limited by your imagination.

I hope you liked this post, please let me know what is the expansion idea that you like to see and why. Please don’t forget to share and subscribe if you haven’t already. Stay tune for the next episode.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.