Project tutorial
Amazon Dash Replenishment Pens

Amazon Dash Replenishment Pens

A pen holder that determines when there are two or less pens and triggers an Amazon pen set replacement.

  • 3 respects

Components and supplies

Necessary tools and machines

09507 01
Soldering iron (generic)

Apps and online services

About this project

I used the Arduino MKR1000 as the microcontroller to create my Pen Replacement system. I used the MKR1000 because it is wifi enabled and there is a great resource for connecting to Amazon DRS with an internet connected Arduino. Make sure that the Arduino software is updated completely before adding the AmazonDRS, AmazonJSON and Wifi101 resources. When I ran it with an older version of the Arduino software, I got a bunch of errors that prevented me from compiling the code.

The tutorial on the website: is very helpful but there are a couple notes that isn't mentioned that caused me some frustration. Within the Security Profile>Web Settings, make sure that you add to Allowed Return URLs. When it gets to the part about AmazonTokens.h, you need to remember to clear the refresh_token, the authorization_grant_code and the access_token before you run the authCodeGrant code to get the refresh_token. Besides that, the github resource is very well written and concise that covers almost everything that you need to get Amazon DRS setup on your MKR1000.

Limit Switches

Before I set it up for the limit switches for my project I tested it with what the example suggested which was a single pushbutton onto pin 12. This was found in the examples under amazonDashButton example.

I wanted to use limit switches instead of photoresistors or a mass sensor for a couple reasons - photoresistors require analog inputs so if I wanted to scale the device to detect 24 pens instead of 5 then I would need a lot more analog inputs. Photoresistors would also require me to autoadjust the system based on outside light. If the system was in a drawer or someplace very dark, the photoresistor wouldn't do it's job very well. I didn't use a mass sensor because if a user stacks a bunch of stuff on top of the pens (like in an office environment) then there would be no indication about whether it was random stuff or something specific.

I bought some limit switches online that require 5VDC, ground and outputs a normally closes signal. Normally closed signals give a 5VDC signal when the limit switch isn't pressed and 0VDC when the switch is pressed. There is an LED on the switch to indicate when the switch is pressed.

I used five limit switches and put them in a row so that each one lines up with a pen. I used quick drying clay to make a shape that would line up the pens with the limit switches. The limit switches were larger than I had initially hoped so because of that, I decided that this project was more of a proof of concept rather than a fully fledged project idea.

Modified Code

See the modified code that I wrote and the comments in the code for how my logic works. Below are images of the results are from running the code.

When the system requests a replacement, it asks Amazon to send an email to the user to notify them that the replenishment is being ordered and is on its way.


Amazon Multiple Limit Switch CodeArduino
Adapted from the Amazon Dash Button example from the instructions I followed, this was changed so that it would take in five inputs and convert it to a number and perform logic based on the number/state.
#include <AmazonDRS.h>
#include <AmazonTokens.h>

#include <WiFi101.h>

#include "AmazonDRS.h"

AmazonDRS DRS = AmazonDRS();

//WiFi creds ----------------------------------------------------------------------------------
char ssid[] = "WiFi"; //  your network SSID (name)
char pass[] = "password";    // your network password (use for WPA, or use as key for WEP)

#define slotNumber 1 //This will vary for multi slot devices - dash buttons typically only serve one product/slot

int y = 0;
int x = 0;

int input0 = 6; // pin for the first input
int input1 = 7; // pin for the second input
int input2 = 8; // pin for the third input
int input3 = 9; // pin for the fourth input
int input4 = 10;// pin for the fifth input

static String slotStatus = "true"; //boolean which depicts if slot is available for replenishment
static String slotId = "abddw91c-cdd1-3asd-b937-bef481699f5d";     //unique slot id ex: 0a5038b7-7609-4b81-b87e-3e291f386324
int status = WL_IDLE_STATUS;

void setup() {

  pinMode(input0, INPUT); // set the first DI/DO as an input
  pinMode(input1, INPUT); // set the second DI/DO as an input
  pinMode(input2, INPUT); // set the third DI/DO as an input
  pinMode(input3, INPUT); // set the fourth DI/DO as an input
  pinMode(input4, INPUT); // set the fifth DI/DO as an input
   #ifdef ESP8266 
   WiFiClientSecure client;
   WiFiSSLClient client;
   //Start up DRS

  //connect to WiFi
  Serial.println("Initializing DRS... connecting to WiFi");
  while (status != WL_CONNECTED) {
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    status = WiFi.status();

  Serial.print("Connected to ");

  //initialize slots
  DRS.retrieveSubscriptionInfo();  //check slot statuses

  slotStatus = DRS.getSlotStatus(slotNumber);
  slotId = DRS.getSlotId(slotNumber);

void loop() {

  //Check for button push on the arduino dash button
  //if the slot status is true proceed to request replenishment for the associated slot

  // wait a second
  // set x to be the sum of all of the inputs
  x = convertDigitalRead(input0) + convertDigitalRead(input1) + convertDigitalRead(input2) + convertDigitalRead(input3) + convertDigitalRead(input4);

  // print the value of x so that we know how many inputs are triggered
  // if the sum of the triggered inputs are less than 3 and if y is equal to 0 then
  if ((x < 3) && (y == 0))
    // set y to be 1 which stands in as the current state (I don't want multiple orders if the pens aren't replaced)
    y = 1;
    // print that the switch was triggered
    Serial.println("Switch was triggered");
       //Check if slot is available, if so replenish

        if(slotStatus == "true")   //if the product in slot are available
            //we have a match! replenish the products associated with that slot!
          Serial.print("Sorry, slot ");
          Serial.println(" is not available at this time");
  else if ((x > 3) && (y = 1))  // if y = 1 (I've already ordered one and I'm waiting for a replacement) and x is greater than 3, this means that the pens have been replaced and I can wait to order another replacement
    // set y to zero to reset the replacement state.
    y = 0;


// set a function called convertDigitalRead that takes in an integer and outputs an integer
int convertDigitalRead(int in)
  // the limit switch is normally closed so if it's low then I want to return a 1
  if (digitalRead(in) == LOW)
    return 1;
  // if the limit switch is closed then return a 0
    return 0;


Schematic layout for Pen replacement
Untitled sketch schem 1fdocjwrxj


Similar projects you might like

Amazon Dash Button for Arduino!

Project tutorial by Brian Carbonette

  • 24 respects

Smart 3D Printer Filament Counter (FilamentBot)

Project tutorial by ArtSuzhou

  • 24 respects

Amazon DRS Promise: Never Miss Coffee Break Again!

Project tutorial by coffee-drinkers

  • 3 respects

Dash Lid

Project tutorial by claude garrett

  • 11 respects

Arduino-Powered Smart Light (Works with Amazon Echo)

Project tutorial by Tinker Project

  • 29 respects

Amazon Kitchen DRS

Project in progress by Tanner Stinson

  • 20 respects
Add projectSign up / Login