Project tutorial
Arduino-Based Bitcoin Candy Vending Machine

Arduino-Based Bitcoin Candy Vending Machine

Implement your own Bitcoin candy vending machine that accepts testnet Bitcoins and dispenses goods.

  • 4,234 views
  • 18 comments
  • 42 respects

Components and supplies

Necessary tools and machines

Lasercutter
Laser cutter (generic)
Or use the regular saw tools to cut off the wood

Apps and online services

About this project

Arduino-based Bitcoin vending machine

Step 1: Project Idea

I have been impressed by how Bitcoin technology (Blockchain) works, and how you can easily send/receive funds. And since I am a maker, so I asked myself "What about easily connecting hardware projects to such network making these hardware accepts bitcoins?" Then I started by implementing the first Arduino Bitcoin library called Koyn.

Rapidly wrapping up, Koyn is an Arduino library that helps Arduino makers to build their hardware projects and connect them to the Bitcoin network (currently supporting test network) and accept bitcoins, making it easy for the users to take hardware actions whenever they spend/receive bitcoins. You can check the link for the documentation here.

Step 2: Implementation

To get my hand dirty, I made my first application using Koyn library "A Bitcoin Candy Vending Machine." And with simple provided API methods, you can easily track your Bitcoin addresses over the network.

To be honest writing the code didn't take any time, as Koyn's API interface provides most of the functionality to control your funds, while connecting the hardware parts is a little bit tricky, and as a maker you may wonder "Why would I use two development boards in one proect?"

The answer is that I already had a Wemos (ESP8266) WiFi board, which doesn't provide plenty I/O pins to control numerous servo motors + LCD screen + Push Buttons. Then I came up with a simple hack! I decided to let all the controls and interfaces handled by an Arduino Mega board, and of course the funds controlled by the WiFi Wemos board.

I wrote two Arduino sketches (codes), one for the Arduino Mega controlling servos, push buttons and LCD, and the other sketch to track my bitcoin address using koyn library.

Then connected both boards over UART to easily communicate, allowing the Mega to update the funds over the LCD and take input from the push buttons to control the servos dispensing goods.

Making both boards communicate was also a little bit tricky, so I used Arduhdlc library as a serial protocol.

Special thanks to "Dejan Nedelkovski" for providing the designs of the machine and a step by step procedure to build your own.

Step 3: Connections

Now the most interesting part! connecting hardware together.

I also decided to place all the components on the back of the vending's machine door. "All connections provided in the schematics section."

Step 4: Code

Provided in the code section, you can find both Wemos and Arduino Mega codes.

As I mentioned before, each board is doing its job according to the code provided, so Wemos board is connected to the internet (Bitcoin network) listening to my Bitcoin address sending any funds updates to the Mega.

On the other side the Mega takes the hardware actions by checking the balance and assign each buttons balance, and outputs users messages over the LCD and finally controlling the servos.

Make sure to download ESP8266 core to compile and upload the code over the Wemos board.

Also I have provided Arduhdlc library in the code section, make sure to download the library and place it under your Arduino directory "C:\Users\"name"\Documents\Arduino\libraries"

Final

That was pretty much how I built my first decentralized Bitcoin vending machine, if you have any comments please leave it bellow in the comments section.

Code

BitcoinVendingMachine(Wemos)Arduino
This is the code for Wemos board
#define MAX_HDLC_FRAME_LENGTH 32


#include "Arduhdlc.h"
#include "Koyn.h"

BitcoinAddress myAddress("muutGNozJxnA3z4nMrcKvZvSnz8WHFWNb2", ADDRESS_ENCODED);

Arduhdlc hdlc(&byteSender, &frameCallback, MAX_HDLC_FRAME_LENGTH);

void byteSender(uint8_t data) {
  Serial.write(data);
}

void frameCallback(const uint8_t *data, uint16_t length) {

}

void setup() {
  Serial.begin(115200);
  // CONNECT TO WIFI
  wifiConnect();
  Koyn.begin();
  Koyn.onNewTransaction(&paymentCallback);
  //  Koyn.onNewBlockHeader(&blockCallback);
}

void loop() {
  if (Koyn.isSynced()) {
    Koyn.trackAddress(&myAddress);
  }
  Koyn.run();
}

void paymentCallback(BitcoinTransaction tx) {
  Serial.println("Got Transaction");
  for (int j = 0; j < tx.getOutputsCount(); j++)
  {
    BitcoinAddress to;
    tx.getOutput(j, &to);
    if (to == myAddress) {
      Serial.println("My Address");
      uint64_t amount = tx.getOutputAmount(j);
      hdlc.frameDecode((uint8_t *)&amount,8);
      break;
    }
  }
}

void wifiConnect()
{
  WiFi.begin("SSID_NAME", "SSID_PASSWORD");
  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.print("Connected, IP address: ");
  Serial.println(WiFi.localIP());
}
BitcoinVendingMachine(Mega)Arduino
This is the code for Arduino Mega
#include <LiquidCrystal.h> // includes the LiquidCrystal Library
#include <Servo.h>
#include "Arduhdlc.h"

#define MAX_HDLC_FRAME_LENGTH 32

#define button1 13
#define button2 12
#define button3 11
#define button4 10

#define button1_price 1000000
#define button2_price 2000000
#define button3_price 3000000
#define button4_price 4000000

LiquidCrystal lcd(27, 26, 25, 24, 23, 22);
Servo servo1, servo2, servo3, servo4;

Arduhdlc hdlc(&byteSender, &frameCallback, MAX_HDLC_FRAME_LENGTH);

int buttonPressed;

long long balance = 0;

void byteSender(uint8_t data) {
    Serial.write(data);
}

void frameCallback(const uint8_t *data, uint16_t length) {
  if(length == 8){
    balance += *((long long *)data);
  }
}

void setup() {
  Serial.begin(115200);
  lcd.begin(16, 2); 

  servo1.attach(4);
  servo2.attach(5);
  servo3.attach(6);
  servo4.attach(7);

  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP);
  pinMode(button3, INPUT_PULLUP);
  pinMode(button4, INPUT_PULLUP);

  pinMode(9,OUTPUT);
  analogWrite(9,100);
  lcd.clear();
}

void loop() {
  lcd.setCursor(0, 0);
  if(balance<=0){
    lcd.print(" Send Bitcoins! ");}
  else{
    delay(10);
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Select your item");
    lcd.setCursor(0, 1);
    lcd.print("Balance = ");
    lcd.print((float)balance/100000000ul);
    
    while (balance>0) {
      if (digitalRead(button1) == LOW) {
        if(balance>=button1_price){
          balance -=button1_price;
          buttonPressed = 1;
          break;
        }
        else{
          lcd.setCursor(0, 0);
          lcd.print(" Topup balance! ");
          delay(1000);
          lcd.setCursor(0, 0);
          lcd.print("Select your item");
        }
      }
      if (digitalRead(button2) == LOW) {
        if(balance>=button2_price){
          balance -=button2_price;
          buttonPressed = 2;
          break;
        }
        else{
          lcd.setCursor(0, 0);
          lcd.print(" Topup balance! ");
          delay(1000);
          lcd.setCursor(0, 0);
          lcd.print("Select your item");
        }
      }
      if (digitalRead(button3) == LOW) {
        if(balance>=button3_price){
          balance -=button3_price;
          buttonPressed = 3;
          break;
        }
        else{
          lcd.setCursor(0, 0);
          lcd.print(" Topup balance! ");
          delay(1000);
          lcd.setCursor(0, 0);
          lcd.print("Select your item");
        }
      }
      if (digitalRead(button4) == LOW) {
        if(balance>=button4_price){
          balance -=button4_price;
          buttonPressed = 4;
          break;
        }
        else{
          lcd.setCursor(0, 0);
          lcd.print(" Topup balance! ");
          delay(1000);
          lcd.setCursor(0, 0);
          lcd.print("Select your item");
        }
      }
      readFromSerial();
      lcd.setCursor(0, 1);
      lcd.print("Balance = ");
      lcd.print((float)balance/100000000ul);
    }
    
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("Delivering...");
    
    switch (buttonPressed) {
      case 1:
        // Rotate the helical coil, discharge the selected item
        servo1.writeMicroseconds(1000); // rotate
        delay(2000);
        servo1.writeMicroseconds(1500);  // stop
        delay(500);
        break;
        
       case 2:
        // Rotate the helix, push the selected item
        servo2.writeMicroseconds(1000); // rotate
        delay(2000);
        servo2.writeMicroseconds(1500);  // stop
        delay(2000);
        break;
  
        case 3:
        // Rotate the helix, push the selected item
        servo3.writeMicroseconds(1000); // rotate
        delay(2000);
        servo3.writeMicroseconds(1500);  // stop
        delay(500);
        break;
  
        case 4:
        // Rotate the helix, push the selected item
        servo4.writeMicroseconds(1000); // rotate
        delay(2000);
        servo4.writeMicroseconds(1500);  // stop
        delay(500);
        break;
    }
    
    lcd.clear(); // Clears the display
    lcd.setCursor(0, 0);
    lcd.print("Item delivered!"); // Prints on the LCD
    delay(1000);
  }
  readFromSerial();
}

void readFromSerial() {
    while (Serial.available()) {
        hdlc.charReceiver(Serial.read());
    }
}
Koyn
A fully decentralized and trustless Bitcoin light (aka SPV) client implementation for the Arduino platform based on Electrum protocol.
Arduhdlc
A simple library protocol for Arduino boards

Custom parts and enclosures

Vending Machine
vending_machine_QCLeoxV0j0.dxf

Schematics

Vending Machine
Vending machine 7rgzwyq9rf

Comments

Similar projects you might like

Candy Dispenser with Google Assistant

Project tutorial by Arduino “having11” Guy

  • 15,482 views
  • 1 comment
  • 51 respects

Snacks Vending Machine Powered By Arduino

Project tutorial by Sevenmojoe

  • 17,643 views
  • 13 comments
  • 84 respects

Electronic Weighing Machine

Project tutorial by Boaz Lawnce

  • 9,087 views
  • 7 comments
  • 19 respects

Candy Dispenser

Project tutorial by Hada Amira

  • 7,530 views
  • 7 comments
  • 23 respects

DIY Vending Machine using Arduino & 1Sheeld

Project tutorial by Mahmoud Ahmed

  • 7,449 views
  • 7 comments
  • 32 respects

IoTrix (Smart Advertisement LED Board)

Project tutorial by Ravi Parmar

  • 2,265 views
  • 0 comments
  • 20 respects
Add projectSign up / Login