Internet of Things in Healthcare

Today I will talk about you how the Internet of Things can be used in the healthcare sector, and I will show you how to improve medication adherence with a smart and connected device.

In the US, it is estimated that nearly 75% of adults do not adhere to prescriptions of medicine, and the total economic burden of medication non-adherence is estimated to be $100–300 billion annually ( That’s including the cost of hospitalizations and emergency department visits that could have been prevented with proper adherence. So let’s create a smart and connected device that could do automatic reminders as well as share the adherence. Studies show that adherence will increase from 71% to 98% using a similar device, and an important part of the reason is that the sharing can assist the doctor (to make better decisions), add social support (family and friends can help), and simplify (the pharmacy can be automatically notified about needed) refills ( Some companies are already experimenting with this concept, and some examples are Vitality ( and AdhereTech (

This is what you need:

  • Box for pill bottle and circuit
  • Arduino (Mini Pro, 3.3V)
  • WiFi module (ESP8266)
  • Photocell sensor
  • LED
  • 10kΩ resistor
  • 68Ω resistor
  • Breadboard
  • 9 jumper wires

Pill reminder

For more information about the ESP8266 module, please see my previous video called “Internet of Things in Transportation”. I started by drilling the holes for the pill bottle and the LED, and then prepared it for the circuit. To setup the circuit, you start by disconnecting everything from its power source, and then connect the Arduino VCC to the LED anode (long), sensor, and both the WiFi module’s VCC and CH_PD. Then you connect the Arduino GND to the LED cathode (short), and the GND of the sensor and WiFi. Connect Arduino pin A0 to sensor over a 10kΩ pull up resistor, and Arduino pin 10 to LED over 220Ω resistor. Then connect Arduino pin 2 to the WiFi transmit (TXD) pin, and Arduino pin 3 to the WiFi receive (RXD) pin.

This is the Arduino code to keep track of when to take a pill and turn on the LED until the pill is taken…

…and after the initial includes (on lines 1-2), a variable is created for the Software Serial (on line 4). Then a number of constants are define for the pin numbers (on lines 5-6) and light threshold (on line 7), as well as a number of variables (on lines 8-12). In the setup, the LED is initiated (on lines 16-17) and the software serial communication is initiated (on line 18). Then the WiFi module is queried for an IP address (on line 19). If there isn’t an IP address (checked on line 20, which means that it’s not connected to the access point), we connect to the access point (on lines 22 and 24). The eternal loop contains the following state machine…

State machine

…and it starts in the first state (0) by checking if a day has passed since a pill was taken (on line 34), and if so, changes to the second state (on line 36). The second state (1) blinks the LED (on lines 41-46) and checks if the bottle is removed, i.e. the light sensor is above the threshold (on line 47). If so, the LED is turned on (on line 50) and waits for the bottle to be put back (on line 51), and then changes to the third state (on line 49). The third state (2) checks if the bottle is put back (on line 55), and if so, turns off the LED (on line 58), calculate the time passed since a pill was last taken (on line 59), and save the new time (on line 60). The IP connection is started (on line 61), I check for any connection errors (on line 63), and create the HTTP request (on line 64). We define the length of the request (on lines 65-66), and if the WiFi module is ready for the request (checked on line 68), we send the request (on lines 70-71). Lastly, we close the connection (on line 74), and return to the first state.

You sign up for a ThingSpeak account (on, and create a new channel, to get the API key for writing that you need to insert into the code. As you are removing the pill bottle (i.e. taking a pill), the will start seeing the values in the chart on the channel page. Note that ThingSpeak is open source (, so you could also set it up on your own server.

That’s how the Internet of Things can be used in the healthcare sector.