Raspberry Pi : Bitcoin Mining

I have always found Bitcoin to be intriguing... Yeah, I know it is probably not cost effective... Yeah, I know the currency crashes every couple of months... Yeah, I know it will probably be illegalized soon... But somehow, using compute cycles to generate currency out of thin air just pushes my geek buttons! And when I realized I could use a Raspberry Pi to run my miner, the deal was sealed.

I took the plunge a few weeks ago and purchased an ASIC miner. (For those unfamiliar with the history of mining Bitcoin, the difficulty of solving each block increases every other week or so. Early on you could mine with just your computer's CPU. But fairly quickly the difficulty increased to the point where a single CPU was no longer effective, so miners switched to using the highly parallel GPU cores found in modern graphics cards.

Now, the difficulty has increased to the point that even GPU mining is obsolete and miners have turned to using custom ASIC (Application Specific Integrated Circuit) chips that implement the necessary hashing algorithms in hardware giving it the extreme speeds need to achieve profitable hashing rates.  

For my experimentation with Bitcoin I settled on a second generation, USB powered ASIC miner, the AntMiner U1. (Selling for about $60 on Amazon.) Rated for a very respectable 1.6 GHps (with overclocking to up to 2 GHps and beyond) it is a very reasonable way to get involved in Bitcoin mining. At least at a hobby level.

Full disclaimer. Mining Bitcoin is not likely to ever be profitable without some significant investment in hardware or a significant rise in the value of Bitcoin. I undertake this effort as a hobby, not as a alternate source of income. If you want to project the profitability of mining Bitcoin with a specific hardware setup, you should consulte this site. It most cases, you will be very forunate if you reach a break even point before the difficulty catches up with you. That, however, does nothing to detract from the coolness of being a Bitcoin miner. So press on!

According to the specifications, my miner draws a full 500 milliamps from the USB port. Since I wanted to use my Raspberry Pi as the controller, this presents a problem since the Raspbery Pi only can deliver about 100 milliamps combined across both USB ports. This necessitated a search for a powered USB hub which could not only deliver 500 milliamps to each port, but that was also compatible with the Raspberry Pi!

After a long search, I settled on this one from Amazon. To confirm it worked, I first logged into my Pi and ran this command:

lsusb  

Which listed the following devices:

Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.  
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub  
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.  

These are to be expected. There is a onboard hub (Device 001) which controls two ports (Device 002 and Device 003).

I then powered up the hub, plugged in my miner, and plugged the whole assembly into my Pi. Running lsusb again, I see the additional entries:

Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.  
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub  
Bus 001 Device 008: ID 0835:8502 Action Star Enterprise Co., Ltd  
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.  
Bus 001 Device 004: ID 0835:8500 Action Star Enterprise Co., Ltd  
Bus 001 Device 005: ID 0835:8501 Action Star Enterprise Co., Ltd  
Bus 001 Device 006: ID 10c4:ea60 Cygnal Integrated Products, Inc. CP210x UART Bridge / myAVR mySmartUSB light  
Bus 001 Device 007: ID 0835:8500 Action Star Enterprise Co., Ltd  

Now I am ready to set up the miner!

Mining on anything less than an industrial scale requires participation in a mining pool. A pool harnesses the hashing power of all its members to collectively solve a block. Once solved, the block reward is distributed to all the members proportionate to their respective contributions. A pool usually charges a small fee to support the infrastructure needed to run the pool, but that is usually only 2% or 3% of the final reward.

To start mining with your ASIC miner and Raspberry Pi you need to join a pool and create a "worker" which is an account to which your mining will submit solutions. I recommend Slush's Pool.

Most mining setups utilize an open source program called cgminer. Used as a controller for a wide array of different hardware, cgminer handles communication of work units and solutions to and from a pool's servers.

The Antminer U1 uses a custom version of cgminer with drivers for it preinstalled. (If you are using a different miner, your mileage may vary.) To get started I created a separate directory in my home directory to hold all things bitcoin:

mkdir bitcoin  
cd bitcoin  

The software is available on github:

git clone https://github.com/AdvancedStyle/cgminer  
cd cgminer  

The software unfortunately requires a lengthy build and configuration process that has additional software dependencies. Some of these I found online, and some I found by trial and error. Below is a list of all the ones I needed, running the latest Raspberry Pi firmware, to save you the trouble. Once again, your mileage may vary.

sudo apt-get install libusb-1.0  
sudo apt-get install libudev-dev  
sudo apt-get install dh-autoreconf  
sudo apt-get install libcurl4-openssl-dev  

Once all these are successfully downloaded and installed, you can start the cgminer build process. This takes a long time. Easily an hour. Do not be worried if it seems to hang.

./autogen.sh

Once that completes, then you need to enable support for your particular device:

./configure –enable-bmsc

Then one final build step:

make -j 6  

One final tweak recommended for Raspberry Pi users:

sudo vi /boot/cmdline.txt  

Then add the following to the end of the first line:

slub_debug=FP  

According to online wisdom, this improves the stability of the miner. I cannot vouch for that one way or another, but just included it here for completeness.

You now have an executable capable of running cgminer and controlling your Antminer U1! The command to run the miner is as follows:

./cgminer --bmsc-options 115200:20 -o http://api.bitcoin.cz:8332 -u <YOUR WORKER USERNAME> -p <YOUR WORKER PASSWORD> --bmsc-freq 0781

The bmsc-options are mysterious, but necessary for this miner. I did not ask any questions.

The -o parameter is the URL of your mining pool. The -u and -p< options are for your worker username and password respectively. Finally the --bmsc-freq option is a custom parameter to the Antminer U1 that sets the clock speed.

0781 runs at the standard 1.6 Ghps  
0881 = 1.8 Ghps  
0981 = 2 Ghps  

According to the documentation and online wisdom, 2 Ghps is the most you should run the Antminer U1 without significant external cooling. The clock settings go higher (all the way up to 4 GHps according to some reports) but require significant external cooling and run the risk of causing faults on the chip or even permenant damage.  I personally run my at 0881 which is a solid compromise between performance and stability. (I also added a CPU style heatsink to my Antminer U1 for good measure.)

If you are intent on overclocking you should investigate getting a heatsink (with the proper thermal tape or thermal adhesive!), and a fan. If you are going to push the envelope, you should look at creating a wind tunnel to focus your fan on the chip, or try something more exotic (like a mineral oil bath). Sound off in the comments with any cooling rigs you experiment with!

Running the command as describe above will run it interactively in the terminal. Since you are not likely to want to stay logged into your Pi while you mine, I would suggest the following changes to the command:

nohup ./cgminer --bmsc-options 115200:20 -o http://api.bitcoin.cz:8332 -u <YOUR WORKER USERNAME> -p <YOUR WORKER PASSWORD> --bmsc-freq 0781 &  

The nohup causes the program to persist in executing even if the originating user logs out. The trailing & causes the program to execute in the background, freeing your terminal session to do other things (such as log out.)

I have been mining intermittently for about three weeks at 1.8 Ghps and have earned 5/100s of a Bitcoin or, at current prices, about $4 worth. Considering the $80 of hardware I have acquired, I may never reach the break even point. But that is beside the point...

A few final tips I have learned:

  1. Do not attach a Raspberry Pi camera to your Raspberry Pi while mining. The extra power draw and / or something with the camera drivers makes the miner much less stable and induces frequent crashes.

  2. If possible, I recommend buying a microSD card for your Raspberry Pi and using an adapter such as this one. Over time the pins on the Pi's card reader become worn down causing it to occasionally crash the OS due to a bad read. This wrecks havoc with your efforts to keep a good mining streak going. The microSD adapter I listed above does not have recessed pins like all SD cards have and therefore hold a good connection for a long time.

  3. Ensure your power cord is firmly attached and not worn out. I had a number of unexplained network crashes due to a loose power cord. While not enough power loss to cause the Pi to shut off, it would periodically kill my network connection, and disrupt my miner.

That is it for now! Questions? Comments? Email me at [email protected]!