Coin Sorter/Counter – Code

Main Arduino Code

#include<printing.h>                           //custom library

LiquidCrystal lcd(12,13,8,9,10,11);            //sets pins for lcd
printing p();
#define SENSORPIN 2                            //dime
#define SENSORPIN2 3                           //nickel
#define SENSORPIN3 4                           //quarter

int sensorState = 0;
int lastState = 0;
int sensorState2 = 0;
int sensorState3 = 0;
int sum;                                                                             // keeps track of current money total
void setup() {      
  pinMode(SENSORPIN, INPUT);                  //initialize sensor as input 
  pinMode(SENSORPIN2, INPUT);                 //initialize sensor as input
  pinMode(SENSORPIN3, INPUT);                 //initialize sensor as input     
  digitalWrite(SENSORPIN, HIGH);              //set initial value for sensor as HIGH
  digitalWrite(SENSORPIN2, HIGH);             //set initial value for sensor as HIGH
  digitalWrite(SENSORPIN3, HIGH);             //set initial value for sensor as HIGH
void loop(){
  sensorState = digitalRead(SENSORPIN);        //reads sensor value
  sensorState2 = digitalRead(SENSORPIN2);      //reads sensor value
  sensorState3 = digitalRead(SENSORPIN3);      //reads sensor value
  if (sensorState == LOW) {                    //checks if sensor 1 beam is broken    
    sum += 10;                                 //adds 10 to the current total

   if (sensorState2 == LOW){                    //checks if sensor 2 beam is broken
    sum += 5;                                   //adds 5 to current total
  if(sensorState3 == LOW) {                     //checks if sensor 3 beam is broken
    sum += 25;                                  //adds 25 to current total

  if (sensorState && !lastState) {
  if (!sensorState && lastState) {
  lastState = sensorState;
void displayLCD(){                                                             //function to display total value

library.cpp – Our custom code library

#include "printing.h"
#inlcude "arduino.h"
#include <stdio.h>
void printing::printingDime(){
	Serial.println("Break - Dime");
void printing::printingQuarter(){
	Serial.println("Break - Quarter");
void printing::printingNickel(){
	Serial.println("Break - Nickel");

library.h – Header file for our custom code library

#ifndef printing_h
#define printing_h

class printing
		void printingDime();
		void printingQuarter();
		void printingNickel();


These are the three files we used to code the project, the first being the main Arduino file, the second being the custom library used in our main Arduino file, and lastly the header file used for the custom library. This code is responsible for detecting breaks in the IR break beams and adding to the count of the total amount in coins that has been sorted. The code for the IR breaks was mostly original with help from the Adafruit documentation/learning section specifically for the IR break beam sensors we used.

Coin Sorter/Counter – Schematic

The hardware we used for this project was an Arduino, Adafruit IR Break Beam Sensor, resistors, and a 16×2 LCD. Originally, we used IR LED lights and a simple IR sensor. This proved to be highly inaccurate and inconsistent. The Adafruit IR break beam sensor was exponentially more reliable and worked all the time. To figure out how to wire these components, we referenced the Arduino documentation. The documentation is extensive and extremely helpful for anything related to Arduino.

Coin Sorter/Counter – Big Picture

The project was done with an intent to build a simple coin sorting and counting machine based on Arduino. Infrared break-beam sensors are used to sense the motion of the coin that send an interrupt signal to Arduino, and the detected coin’s value is displayed on LCD 16×2. The initial step was to construct the machine with cardboard. A slightly slanted slider was attached to the board that let the coins fall smoothly into its designated place. Three different holes were cut depending on the ascending diameter of the coins. For the purpose of efficiency, three coins of varying sizes were considered. The next step was to position the pair of Infrared (IR) break-beam sensors, under each hole to detect the breakage of an IR beam. Receiver was set across from emitter, and both were interfaced to Arduino board with resistors, and soldered wires. Arduino program reads the interrupt caused by the coin and prints the value of each coin as they are inserted in to the slider. After numerous attempts, the goal of sorting and counting the coins with Arduino powered machine was accomplished.