Tweeting coffee machine using Raspbery Pi

Observing that LED will give us knowledge if it is being used or not. I am using a Light Dependent Resistor (LDR) as eyes for the Raspberry Pi. The LDRs change their resistance as per the intensity of light falling upon them. Our goal is to register the LED being lit by driving a GPIO pin on the Raspberry Pi LOW. Below is the schematic of my setup:

It is a voltage divider, with a weak pull up provided by a 150K resistor. The LDR has huge resistance when dark, so the GPIO pin will remain in HIGH state when the LED is dark. Upon lighting it up, the LDR’s resistance falls and the voltage divider tend to go LOW. We watch for that even in a python script on the Raspberry Pi and take action.

Tweeting coffee machine using Raspbery PiFor the above schematic to work, we need to ensure that the LDR is in really shielded from ambient light. I cut a piece of pen and placed my LDR inside it, then painted the tube black to prevent outside light from falling on the LDR. The LDR was hot-glued in position.

Now to the software side, we need a python script that will watch the state of GPIO pin 17 and take some action when the coffee machine brings it to ‘LOW’


and it should look like this:

import RPi.GPIO as GPIO
import time
import os

#adjust for where your switch is connected
buttonPin = 17

while True:
    if (not GPIO.input(buttonPin)):
        #this is the script that will be called (as root)
        os.system("python /home/pi/")

Tweeting coffee machine using Raspbery Pi schematicTo have this run upon booting, we need to edit /etc/rc.local (as root since this is the owner).

sudo nano /etc/rc.local

At the bottom, just above exit 0 we’ll add a call to our script.

python /home/pi/

The notification script that will be run is actually sending us an email:



For more detail: Tweeting coffee machine using Raspbery Pi

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:

Leave a Comment

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

Scroll to Top