Coin Counter in Raspberry Pi (RPi-GPIO)
Asked Answered
N

2

6

I'm having a problem to count coins in raspberry pi B+ usin RPi-GPIO.

This is the code:

 import RPi.GPIO as GPIO

 GPIO.setmode(GPIO.BCM)
 GPIO.setup(23, GPIO.IN)

 while True:
    print(GPIO.input(23))

I called the manufacturer and he said the Coin Counter sends a pulse with value 1 constantly and the value 0 when the coin down. But in the console is showing 0 and 1 at random. And nothing change when coin dropped.

4 wire outputs:

  • Black and Red: are 12v (font)
  • White: is pulse (1 constantly, 0 when drop the coin). It is plugged in raspberry on 23 pin (I choose this pin because is available).
  • Purple: is to count coins on separated led.

Voltage: Font 12v, and pulse (white wire) is 0.19~0.25 volts

Note: Pulse is digital.

I have this Coin Counter:

enter image description here

Natalianatalie answered 8/10, 2015 at 20:15 Comment(11)
Can you show us the specs of the hardware? As the connections of the coin counter. With the python code only we won't be able to help.Descriptive
4 wire outputs: black and red are 12v (font), white is pulse on 23 pin (1 constantly, 0 when drop the coin) and purple is to count coins on separeted led. Font 12v, and pulse (white wire) is 0.19~0.25 volts, and i used 10k resistor in this wire. And i use Raspberry pi B+Natalianatalie
What are the specs of the pulse? Why you have chose pin 23? Can you expand with more information?Descriptive
I edited/updated the question.Natalianatalie
Does the coin counter pulse works with the same voltage as RP GPIO input pins?Descriptive
No, pulse between 0.19v and 0.25v. low voltageNatalianatalie
@lilezek, i understand, the counter doesnt work at the same voltage of RPi, i contacted the manufacter and he says: "doenst matter the voltage, the 0 and 1 is always send." this make sense?Natalianatalie
Isn't that a problem? Expected voltage should be between 3.3v and 5v I guess.Descriptive
So I need to make another circuit in a protoboard? when energyze 0.2v in 3.3v?Natalianatalie
@Descriptive i see in (mosaic-industries.com/embedded-systems/microcontroller-projects/…), gpio pins aceppt 0v ~ 3.3vNatalianatalie
Let us continue this discussion in chat.Descriptive
R
1
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
counterPin=23
GPIO.setup(counterPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
    input_state = GPIO.input(counterPin)
    if input_state == False:
        print('coin dropped')
Rhoades answered 9/10, 2015 at 2:51 Comment(2)
its good, but i saw the problem, i need to read the pulses on 30ms... i can change the pulse on the coinacceptor (30ms;50ms;100ms)Natalianatalie
It looks like a fun project best of luck with it!Rhoades
B
0
again=True

Total=0

Wrongs=0

Corrects=0


Total_bags=0

Wrong_bags=0

Correct_bags=0

coins=['1p','2p','5p','10p','20p','50p','£1','£2']

bag_value=[1.00,1.00,5.00,5.00,10.00,10.00,20.00,20.00]

bag_amount=[100,50,100,50,50,20,20,10]

bag_weight=[3.56,7.12,3.25,6.5,5.0,8.0,8.75,12.0]

coins_len=len(coins)

Master_list=list()

CoinCount_list=list()

Tilte_list=['Name','Coin','Weight','Accurate']

Master_list.append(Tilte_list)

with open("Master.txt","w") as f: 
    f.write("{}".format(Master_list))

while True:

    again=True


    what=input("\nMenu\n1)\tAdd a bag\n2)\tStats\n")

    CoinCount_list=list()
    if what=='1':

        name=input("What is your name\n")

        CoinCount_list.append(name)

        with open("CoinCount.txt","w") as f: 
            f.write("{}".format(CoinCount_list))


        with open("CoinCount.txt","w") as f: 
            f.write("{}".format(CoinCount_list))

        Percent=0

        Wrongs=0

        Corrects=0 


while again:

Coin

        coin_input=input("What type of coin do you have\n")
        for i in range (0,coins_len):
            if coin_input==coins[i]:

                Coin=coins[i]
                valid=input("That is valid, is that what you wanted\n")

                if valid=="Yes":


                    CoinCount_list.append(coins[i])

                    with open("CoinCount.txt","w") as f: 
                        f.write("{}".format(CoinCount_list))

                    while True:
                        try:    
                            weight=int(input("What is the weight of the bag\n"))
                            break
                        except ValueError:
                            print("Oops!  That was no valid number.  Try again...\n")
                    amountCoin=weight/bag_weight[i]

Weight

                    CoinCount_list.append(weight)

                    with open("CoinCount.txt","w") as f: 
                        f.write("{}".format(CoinCount_list))

                        if bag_amount[i] == amountCoin:

                            Total_bags=Total_bags+1

                            again=input("Thank you would you like to add another bag\n")

                            CoinCount_list.append('Correct')

                            with open("CoinCount.txt","w") as f: 
                                f.write("{}".format(CoinCount_list))

                            Total=bag_value[i]+Total
                            Correct_bags=Correct_bags+1

                            Corrects=Corrects+1

                            if again== 'Yes':
                                a='b'



                            else:
                                again=False


                                Master_list.append(CoinCount_list)

                                with open("Master.txt","w") as f: 
                                    f.write("{}".format(Master_list))







                        elif bag_amount[i] > amountCoin:
                            print("You have ",amountCoin,"coins, add ",amountCoin-bag_amount[i], "coin\n")

                            Wrong_bags=Wrong_bags+1
                            Total_bags=Total_bags+1

                            Wrongs=Wrongs+1



                        elif bag_amount[i] <amountCoin:
                            print("You have ",amountCoin,"coins, take away ",amountCoin-bag_amount[i], " coin\n")

                            Wrong_bags=Wrong_bags+1
                            Total_bags=Total_bags+1

                            Wrongs=Wrongs+1



if what=='2':
        print("")
        print("  We have check",Total_bags," ")
        print(" ",Correct_bags,"Correctly")
        print(" ",Wrong_bags,"Incorrectly")
        print("  We have raised £",Total," ")
        print("")
        with open("Master.txt") as f:
           rd=f.readlines()
        #print (rd)

        print(*Master_list, sep='\n')

        f = open("CoinCount.txt", "w")
        f.write( str(Master_list) ) 
        f.close()
Bowes answered 2/10, 2018 at 13:28 Comment(1)
I am not familiar how readable the code is, how ever, I think you should comment or add to this comment itself some explanation how your code works.Pottle

© 2022 - 2025 — McMap. All rights reserved.