Tag Archives: hackspace

Monitor air quality with a Raspberry Pi

via Raspberry Pi

Add a sensor and some Python 3 to your Raspberry Pi to keep tabs on your local air pollution, in the project taken from Hackspace magazine issue 21.

Air is the very stuff we breathe. It’s about 78% nitrogen, 21% oxygen, and 1% argon, and then there’s the assorted ‘other’ bits and pieces – many of which have been spewed out by humans and our related machinery. Carbon dioxide is obviously an important polluter for climate change, but there are other bits we should be concerned about for our health, including particulate matter. This is just really small bits of stuff, like soot and smog. They’re grouped together based on their size – the most important, from a health perspective, are those that are smaller than 2.5 microns in width (known as PM2.5), and PM10, which are between 10 and 2.5 microns in width. This pollution is linked with respiratory illness, heart disease, and lung cancer.

Obviously, this is something that’s important to know about, but it’s something that – here in the UK – we have relatively little data on. While there are official sensors in most major towns and cities, the effects can be very localised around busy roads and trapped in valleys. How does the particular make-up of your area affect your air quality? We set out to monitor our environment to see how concerned we should be about our local air.

Getting started

We picked the SDS011 sensor for our project (see ‘Picking a sensor’ below for details on why). This sends output via a binary data format on a serial port. You can read this serial connection directly if you’re using a controller with a UART, but the sensors also usually come with a USB-to-serial connector, allowing you to plug it into any modern computer and read the data.

The USB-to-serial connector makes it easy to connect the sensor to a computer

The very simplest way of using this is to connect it to a computer. You can read the sensor values with software such as DustViewerSharp. If you’re just interested in reading data occasionally, this is a perfectly fine way of using the sensor, but we want a continuous monitoring station – and we didn’t want to leave our laptop in one place, running all the time. When it comes to small, low-power boards with USB ports, there’s one that always springs to mind – the Raspberry Pi.

First, you’ll need a Raspberry Pi (any version) that’s set up with the latest version of Raspbian, connected to your local network, and ideally with SSH enabled. If you’re unsure how to do this, there’s guidance on the Raspberry Pi website.

The wiring for this project is just about the simplest we’ll ever do: connect the SDS011 to the Raspberry Pi with the serial adapter, then plug the Raspberry Pi into a power source.

Before getting started on the code, we also need to set up a data repository. You can store your data wherever you like – on the SD card, or upload it to some cloud service. We’ve opted to upload it to Adafruit IO, an online service for storing data and making dashboards. You’ll need a free account, which you can sign up for on the Adafruit IO website – you’ll need to know your Adafruit username and Adafruit IO key in order to run the code below. If you’d rather use a different service, you’ll need to adjust the code to push your data there.

We’ll use Python 3 for our code, and we need two modules – one to read the data from the SDS011 and one to push it to Adafruit IO. You can install this by entering the following commands in a terminal:

pip3 install pyserial adafruit-io

You’ll now need to open a text editor and enter the following code:

This does a few things. First, it reads ten bytes of data over the serial port – exactly ten because that’s the format that the SDS011 sends data in – and sticks these data points together to form a list of bytes that we call data.

We’re interested in bytes 2 and 3 for PM2.5 and 4 and 5 for PM10. We convert these from bytes to integer numbers with the slightly confusing line:

pmtwofive = int.from_bytes(b’’.join(data[2:4]), byteorder=’little’) / 10

from_byte command takes a string of bytes and converts them into an integer. However, we don’t have a string of bytes, we have a list of two bytes, so we first need to convert this into a string. The b’’ creates an empty string of bytes. We then use the join method of this which takes a list and joins it together using this empty string as a separator. As the empty string contains nothing, this returns a byte string that just contains our two numbers. The byte_order flag is used to denote which way around the command should read the string. We divide the result by ten, because the SDS011 returns data in units of tens of grams per metre cubed and we want the result in that format aio.send is used to push data to Adafruit IO. The first command is the feed value you want the data to go to. We used kingswoodtwofive and kingswoodten, as the sensor is based in Kingswood. You might want to choose a more geographically relevant name. You can now run your sensor with:

python3 airquality.py

…assuming you called the Python file airquality.py
and it’s saved in the same directory the terminal’s in.

At this point, everything should work and you can set about running your sensor, but as one final point, let’s set it up to start automatically when you turn the Raspberry Pi on. Enter the command:

crontab -e

…and add this line to the file:

@reboot python3 /home/pi/airquality.py

With the code and electronic setup working, your sensor will need somewhere to live. If you want it outside, it’ll need a waterproof case (but include some way for air to get in). We used a Tupperware box with a hole cut in the bottom mounted on the wall, with a USB cable carrying power out via a window. How you do it, though, is up to you.

Now let’s democratise air quality data so we can make better decisions about the places we live.

Picking a sensor

There are a variety of particulate sensors on the market. We picked the SDS011 for a couple of reasons. Firstly, it’s cheap enough for many makers to be able to buy and build with. Secondly, it’s been reasonably well studied for accuracy. Both the hackAIR and InfluencAir projects have compared the readings from these sensors with more expensive, better-tested sensors, and the results have come back favourably. You can see more details at hsmag.cc/DiYPfg and hsmag.cc/Luhisr.

The one caveat is that the results are unreliable when the humidity is at the extremes (either very high or very low). The SDS011 is only rated to work up to 70% humidity. If you’re collecting data for a study, then you should discard any readings when the humidity is above this. HackAIR has a formula for attempting to correct for this, but it’s not reliable enough to neutralise the effect completely. See their website for more details: hsmag.cc/DhKaWZ.

Safe levels

Once you’re monitoring your PM2.5 data, what should you look out for? The World Health Organisation air quality guideline stipulates that PM2.5 not exceed 10 µg/m3 annual mean, or 25 µg/m3 24-hour mean; and that PM10 not exceed 20 µg/m3 annual mean, or 50 µg/m3 24-hour mean. However, even these might not be safe. In 2013, a large survey published in The Lancet “found a 7% increase in mortality with each 5 micrograms per cubic metre increase in particulate matter with a diameter of 2.5 micrometres (PM2.5).”

Where to locate your sensor

Standard advice for locating your sensor is that it should be outside and four metres above ground level. That’s good advice for general environmental monitoring; however, we’re not necessarily interested in general environmental monitoring – we’re interested in knowing what we’re breathing in.

Locating your monitor near your workbench will give you an idea of what you’re actually inhaling – useless for any environmental study, but useful if you spend a lot of time in there. We found, for example, that the glue gun produced huge amounts of PM2.5, and we’ll be far more careful with ventilation when using this tool in the future.

Adafruit IO

You can use any data platform you like. We chose Adafruit IO because it’s easy to use, lets you share visualisations (in the form of dashboards) with others, and connects with IFTTT to perform actions based on values (ours tweets when the air pollution is above legal limits).

One thing to be aware of is that Adafruit IO only holds data for 30 days (on the free tier at least). If you want historical data, you’ll need to sign up for the Plus option (which stores data for 60 days), or use an alternative storage method. You can use multiple data stores if you like.

Checking accuracy

Now you’ve got your monitoring station up and running, how do you know that it’s running properly? Perhaps there’s an issue with the sensor, or perhaps there’s a problem with the code. The easiest method of calibration is to test it against an accurate sensor, and most cities here in the UK have monitoring stations as part of Defra’s Automatic Urban and Rural Monitoring Network. You can find your local station here. Many other countries have equivalent public networks. Unless there is no other option, we would caution against using crowdsourced data for calibration, as these sensors aren’t themselves calibrated.

With a USB battery pack, you can head to your local monitoring point and see if your monitor is getting similar results to the monitoring network.

HackSpace magazine #21 is out now

You can read the rest of this feature in HackSpace magazine issue 21, out today in Tesco, WHSmith, and all good independent UK newsagents.

Or you can buy HackSpace mag directly from us — worldwide delivery is available. And if you’d like to own a handy digital version of the magazine, you can also download a free PDF.

The post Monitor air quality with a Raspberry Pi appeared first on Raspberry Pi.

Brand-new books from The MagPi and HackSpace magazine

via Raspberry Pi

Hey folks, Rob from The MagPi here! Halloween is over and November has just begun, which means CHRISTMAS IS ALMOST HERE! It’s never too early to think about Christmas — I start in September, the moment mince pies hit shelves.

Elf GIF

What most people seem to dread about Christmas is finding the right gifts, so I’m here to help you out. We’ve just released two new books: our Official Raspberry Pi Projects Book volume 4, and the brand-new Book of Making volume 1 from the team at HackSpace magazine!

Book of Making volume 1

HackSpace magazine book 1 - Raspberry Pi

Spoiler alert: it’s a book full of making

The Book of Making volume 1 contains 50 of the very best projects from HackSpace magazine, including awesome project showcases and amazing guides for building your own incredible creations. Expect to encounter trebuchets, custom drones, a homemade tandoori oven, and much more! And yes, there are some choice Raspberry Pi projects as well.

The Official Raspberry Pi Projects Book volume 4

The MagPi Raspberry pi Projects book 4

More projects, more guides, and more reviews!

Volume 4 of the Official Raspberry Pi Projects Book is once again jam-packed with Raspberry Pi goodness in its 200 pages, with projects, build guides, reviews, and a little refresher for beginners to the world of Raspberry Pi. Whether you’re new to Pi or have every single model, there’s something in there for you, no matter your skill level.

Free shipping? Worldwide??

You can buy the Book of Making and the Official Raspberry Pi Projects Book volume 4 right now from the Raspberry Pi Press Store, and here’s the best part: they both have free worldwide shipping! They also roll up pretty neatly, in case you want to slot them into someone’s Christmas stocking. And you can also find them at our usual newsagents.

Both books are available as free PDF downloads, so you can try before you buy. When you purchase any of our publications, you contribute toward the hard work of the Raspberry Pi Foundation, so why not double your giving this holiday season by helping us put the power of digital making into the hands of people all over the world?

Anyway, that’s it for now — I’m off for more mince pies!

The post Brand-new books from The MagPi and HackSpace magazine appeared first on Raspberry Pi.

HackSpace magazine 12: build your first rocket!

via Raspberry Pi

Move over, Elon Musk — there’s a new rocket maverick in town: YOU!

Rockets!

Step inside the UK rocketry scene, build and launch a rocket, design your own one, and discover the open-source rocket programmes around the world! In issue 12, we go behind the scenes at a top-secret launch site in the English Midlands to have a go at our own rocket launch, find the most welcoming bunch of people we’ve ever met, and learn about centre of gravity, centre of pressure, acceleration, thrust, and a load of other terms that make us feel like NASA scientists.

Meet the Maker: Josef Prusa

In makerception news, we meet the maker who makes makers, Josef Prusa, aka Mr 3D Printing, and we find out what’s next for his open-source hardware empire.

Open Science Hardware

There are more than seven billion people on the planet, and 90-odd percent of them are locked out of the pursuit of science. Fishing, climate change, agriculture: it all needs data, and we’re just not collecting as much as we should. Global Open Science Hardware is working to change that by using open, shared tech — read all about it in issue 12!

And there’s more…

As always, the new issue is packed with projects: make a way-home machine to let your family know exactly when you’ll walk through the front door; build an Alexa-powered wheel of fortune to remove the burden of making your own decisions; and pay homage to Indiana Jones and the chilled monkey brains in Temple of Doom with a capacitive touch haunted monkey skull (no monkeys were harmed in the making of this issue). All that, plus steampunk lighting, LEDs, drills, the world’s biggest selfie machine, and more, just for you. So go forth and make something!

Get your copy of HackSpace magazine

If you like the sound of this month’s content, you can find HackSpace magazine in WHSmith, Tesco, Sainsbury’s, and independent newsagents in the UK from tomorrow. If you live in the US, check out your local Barnes & Noble, Fry’s, or Micro Center next week. We’re also shipping to stores in Australia, Hong Kong, Canada, Singapore, Belgium, and Brazil, so be sure to ask your local newsagent whether they’ll be getting HackSpace magazine. And if you’d rather try before you buy, you can always download the free PDF now.

Subscribe now

Subscribe now” may not be subtle as a marketing message, but we really think you should. You’ll get the magazine early, plus a lovely physical paper copy, which has a really good battery life.

Oh, and twelve-month print subscribers get an Adafruit Circuit Playground Express loaded with inputs and sensors and ready for your next project. Tempted?

The post HackSpace magazine 12: build your first rocket! appeared first on Raspberry Pi.

Electronics 101.1: Electricity basics

via Raspberry Pi

In HackSpace issue 9, Dave Astels helps us get familiar with what electricity is, with some key terms and rules, and with a few basic components. Get your copy of HackSpace magazine in stores now, or download it as a free PDF here.

An animated GIF of Pickachu the Pokemon

tl;dr There’s more to electricity than Pikachu.

Electricity basics

Electricity is fascinating. Most of our technology relies on it: computers, lights, appliances, and even cars, as more and more are hybrid or electric. It follows some well-defined rules, which is what makes it so very useful.

According to Wikipedia, electricity is ‘the set of physical phenomena associated with the presence and motion of electric charge’. And what’s electric charge? That’s the shortage or excess of electrons.

Let’s go back (or forward, depending on where you are in life) to high school science and the atom. An atom is, at a very simplified level, a nucleus surrounded by a number of electrons. The nucleus is (again, viewing it simply) made up of neutrons and protons. Neutrons have no charge, but protons have a positive charge. Electrons have a negative charge. The negative charge on a single electron is the exact opposite of the positive charge on a single proton. The simplest atom, hydrogen, is made from a single proton and a single electron. The net charge of the atom is zero: the positive charge of the proton and the negative charge of the electron cancel – or balance – each other. An atom’s electrons aren’t just in an amorphous cloud around the nucleus: you can think of them as being arranged in layers around the nucleus…rather like an onion. Or perhaps an ogre. This is a very simplified visualisation of it, but it suffices for our purposes.

A diagram of a copper atom and the text '29 Electrons'

Figure 1: A very stylised representation of a copper atom with its electron shell

In a more complex atom, say copper, there are more protons, neutrons, and electrons, and the electrons are in more layers. By default, a copper atom has 29 protons and 35 neutrons in its nucleus, which is surrounded by 29 electrons. The way the electrons are distributed in their layers leaves the copper atom with a single electron in the outermost layer. This is represented in Figure 1 (above). Without getting further into subatomic physics, let’s just say that having that single electron in the outermost layer makes it easier to manipulate. When we put a bunch of copper atoms together to make copper metal (e.g. a wire), it’s easy to move those outermost electrons around inside the metal. Those electrons moving around is electricity. The amount of electrons moving over a period of time is called ‘current’.

A multimeter showing the figure 9.99 with a resistor connected via crocodile clips

A single 10 kΩ resistor reads almost 10 000 ohms (no electrical component is perfect).

We started by talking about electrons and charge. Look back at the Wikipedia definition: ‘presence and motion of electric charge’. Charge is measured in coulombs: 1 coulomb is approximately 6.242 × 1018 electrons. That’s 6 242 000 000 000 000 000 electrons. They’re very small. Actually, this would be -1 coulomb. +1 coulomb would be that many protons (or really, the net lack of that many electrons).

That’s charge. Now let’s consider moving charge, which is far more useful in general (unless your goal is to stick balloons to the wall). Consider some amount of charge moving through a wire. The amount of charge that moves past a specific point (and thus through the wire) over a period of time is called ‘current’ (just like the current in a river) and is measured in amperes, generally just called amps. Specifically, 1 amp is equal to 1 coulomb flowing past a point in 1 second.

Another common term is voltage. You can think of voltage like water pressure; it’s the pressure pushing the electrons (i.e. charge) through a material. The higher the voltage (measured in volts), the faster charge is pushed through, i.e. the higher the current.

The final term is resistance, measured in ohms. Resistance is just what it sounds like. It’s a measure of how much a material resists the movement of electrons. We said that copper allows electrons to move freely. That’s what makes it so common for wires, PCB traces, etc. We say that it is a good conductor. Glass, on the other hand, locks its electrons in place, not letting them move. It’s an example of a good insulator. There are materials that are in between: they let electrons move, but not too freely. These are crucial to making electronics work.

There’s an interesting (and useful) relationship between voltage, current, and resistance called Ohm’s Law (Georg Ohm was the fellow who explored and documented this relationship): the current (denoted I, in amps) flowing through a material is equal to the voltage across the material (denoted V, in volts) divided by the material’s resistance (denoted R, in ohms): I = V/R. This equation is foundational and, as such, very handy.

Lighting up

There aren’t many electronic devices that don’t have at least one LED on them somewhere, especially not gadgety ones. If you look at a simple Arduino Uno, it has LEDs for power, Tx, Rx, and pin 13. The first program using electronic components that most people try is one to blink an LED.

A colour spectrum from red to purple

Figure 2: The colour spectrum

LED stands for light-emitting diode. We’ll come back to diodes in a later instalment; all we need to know right now is that a diode has to go the right way around. So that leaves ‘light-emitting’. That simply means that it gives off light: it lights up. Specifically, it lights up when enough current flows through it. Be careful, though. Put too much current through it and it’ll likely crack in two. Seriously, we’ve done it. Best case scenario, you’ll get a bright pulse of light as it burns out. How much current do they like? 20 milliamps (20mA) is typical. Because an LED is a diode, i.e. a semiconductor (we’ll look at these in more detail in a future instalment too), it defies Ohm’s Law. How? It always has the same voltage across it, regardless of the current flowing through it.

An LED will have a specific Vf (f is for forward, as in ‘forward voltage’), which will be defined in its data sheet.

The voltage varies with the colour of light that the LED emits, but usually between 1.8V and 3.3V. Vf for red LEDs will typically be 1.8V, and for blue LEDs 3V–3.3V. As a rule, LEDs with a higher frequency colour will have a larger Vf. Figure 2 (above) shows the colour spectrum. Colours on the right end are lower in frequency and LEDs emitting those colours will have a lower Vf, while those on the left end have a higher frequency and a higher Vf.

A screenshot of resistor-calculator website

Resistor colour bands show the resistance. Online calculators can help you learn the values.

So an LED will have a fixed Vf, and a typical LED that we’ll use likes about 20mA of current. An LED won’t do anything to limit how much current is flowing through it. That’s what we meant when we said it defies Ohm’s Law.

If we take a blue LED and hooked it to a 3.3V power supply, it will shine happily. Do the same thing with a red LED, and it will blink and burn out. So how do we deal with that? How do we use 3.3V or 5V to make an LED light up without burning out? We simply limit the current flowing through it. And for that, we need a resistor and Ohm’s Law.

Getting protection

Figure 3: An LED with a current-limiting resistor

If we want to power a red LED from a 5V source, we know the following information: current has to be 20mA, Vcc will be 5V, and the voltage across the LED will be 1.8V. Consider the circuit in Figure 3. The voltage across the resistor will be Vcc – Vf, i.e. 5 – 1.8 = 3.2V. We said the current through the LED should be 20mA. Since there is only one path through the circuit that goes through the resistor as well as the LED, all current has to flow through both: whatever amount of current flows through the resistor has to flow through the LED, no more, no less. This is the crucial thing to realise. We can calculate the value of the resistance needed using Ohm’s Law: R = V/I = 3.2V/20mA = 3.2V/0.020A = 160 ohms.

The resistor should have a value of 160 ohms to allow 20mA of current to flow through the LED. Knowing that the 20mA and 1.8V values are approximate and that resistors are not exact (+/- 5 or 10 percent are the most common), we chose a slightly higher-value resistor. Considering common resistor values, go with 180 ohm or 220ohm. A higher-value resistor will allow slightly less current through, which might result in a slightly dimmer light. Try it and see. For practical purposes, simply using a 220 ohm resistor usually works fine.

Parallel lines

In the previous section we connected a resistor and an LED end to end. That’s called a series circuit. If we connected them side by side, it would be a parallel circuit. Consider the circuits in Figure 4.

Figure 4: A – series circuit; B – parallel circuit

We’ll use 5V for Vcc. What is the total resistance between Vcc and GND in each circuit? How much current is flowing through each circuit? What is the voltage across each resistor?

When resistors are connected in series, as in circuit A, the resistances are added. So the two 100 ohm resistors in series have a total resistance of 200 ohms.

When resistors are connected in parallel, as in circuit B, it’s more complex. Each resistor provides a path for current to flow through. So we could use an indirect method to calculate the total resistance. Each resistor is 100 ohms, and has one end connected to 5V and the other to 0V (GND), so the voltage across each one is 5V. The current flowing through each one is 5V/100 ohms = 0.05A, or 50mA. That flows through each resistor, so the total current is 100mA, or 0.1A. The total resistance is then R = V/I = 5V/0.1A = 50 ohms. A more direct way is to use the equation 1/Rt = 1/R1 + 1/R2 + … + 1/Rn, where Rt is the total resistance, and R1, R2, etc. are the values of the individual resistors that are in parallel. Using this, 1/Rt = 1/100 + 1/100 = 2/100 = 1/50. So Rt = 50. This is a quicker way to do it, and only involves the resistor values.

An image of a multimeter

A multimeter can read voltage, ampage, and resistance

Now for current. We know that the series circuit has a total resistance of 200 ohms, so the current will be I = V/R = 5V/200 ohm = 0.025A = 25mA. For one 100 ohm resistor the current is 5V/100 ohm = 0.05A = 50mA. This is expected: if the resistance is lower, there is less ‘resistance’ to current flowing, so with the same voltage, more current will flow. We already computed the current for the parallel circuit: 100mA. This is higher because we know that each resistor has 50mA flowing through it. In a parallel circuit, the currents are added.

A multimeter showing the figure 19.88 with a resistor connected via crocodile clips

Two 10kΩ (kiloohm) resistors in series read (almost) 20kΩ

The final question is what voltage is across each resistor. Let’s look at the parallel circuit first. One end of each resistor is connected to 5V, and the other end of each is connected to 0V (GND). So clearly, the voltage across each one is 5V. In a series circuit it’s different. We can use Ohm’s Law because we’ve calculated the current flowing through each one (0.025A), and that current flows through both resistors. Each resistor is 100 ohm, so the voltage across each one will be V = I×R = 0.025A × 100 ohm = 2.5 V. This makes sense intuitively, since the resistors have the same value and the same current is flowing through both. It makes sense that the voltage across each would be equal, and half of the total. Remember that it’s unlikely to be exactly half, due to the slop in the resistor values.

Let’s do this one more time with unequal resistors. See Figure 5.

Figure 5: A – series circuit; B – parallel circuit

For the series circuit, we simply add the resistances: 100ohm + 82ohm = 182ohm. The current is 5V / 182ohm = 0.0274725A = 27.4725 mA. Because resistors are inexact, it’s safe to call this 27.5mA. The voltages are 100ohm × 0.0275A = 2.75V across the 100 ohm resistor, and 82ohm × 0.275 = 2.25V across the 82 ohm one. The voltages always have to add up, accepting rounding errors. Relative to ground, the voltage at the point between the resistors is 2.75V. What will happen if we make the top resistor smaller (i.e. have a lower resistance)? The total resistance goes down, the current goes up, so the voltage across the 100ohm resistor goes up. This is what’s generally called a voltage divider.

For the parallel circuit we can use 1/Rt = 1/100 + 1/82 = 82/8200 + 100/8200 = 182/8200 = 1/45, so Rt = 45ohm. The total current is 5V / 45ohm = 0.111A = 111mA. For the individual resistors, the currents are 5V / 100ohm = 50mA and 5V / 82ohm = 61mA. Add these up and we have the total current of 111mA. Parallel resistors act as a current divider.

A multimeter showing the figure 4.96 with a resistor connected via crocodile clips

Two 10kΩ resistors in parallel read (almost) 5kΩ.

I encourage you to create these little circuits on a breadboard and measure the resistances, voltages, and currents for yourself.

Resistors in series for a voltage divider, resisters in parallel for a current divider

Consider what happens if we replace the resistor connected to Vcc in a series circuit with a variable resistor. The voltage between the resistors will vary as the value of the resistor does. As the resistance goes down, the voltage goes up. The reverse is true as well: as the resistance goes up, the voltage goes down. One use of this is to replace the variable resistor with a photoresistor. A photoresistor’s value depends on how much light is shining on it (i.e. how many photons are hitting it, to be precise). More light = lower resistance. Now the voltage divider can be used to measure the strength of light. All you need to do is connect the point between the resistors to an analogue input and read it.

Figure 6 Combined parallel and series circuits

We’ve had a brief look at the basic concepts of electricity: charge, current, voltage, and resistance. We’ve also had a closer look at resistors and ways of combining them. We finished with a practical example of a series resistor circuit being used to measure light.

The post Electronics 101.1: Electricity basics appeared first on Raspberry Pi.

Build a social media follower counter

via Raspberry Pi

In this tutorial from HackSpace magazine issue 9, Paul Freeman-Powell shows you how to keep track of your social media followers, and encourage subscribers, by building a live follower counter. Get your copy of HackSpace magazine in stores now, or download it as a free PDF here.

Issues 10 of HackSpace magazine is available online and in stores from tomorrow!

The finished build with all components connected, working, and installed in the frame ready for hanging on the wall

If you run a local business like an electronics shop or a café, or if you just want to grow your online following and influence, this project is a fun way to help you keep track of your progress. A counter could also help contribute to growing your following if you hang it on the wall and actively ask your customers to like/follow you to see the numbers go up!

You’ve probably seen those social media follower counters that feature mechanical splitflap displays. In this project we’ll build a counter powered by RGB LEDs that scrolls through four social profiles, using APIs to pull the number of followers for each account. I’m using YouTube, Twitter, Facebook, and Instagram; you can, of course, tailor the project to your needs.

This project involves a bit of electronics, a bit of software coding, and a bit of woodwork, as well as some fairly advanced display work as we transfer a small portion of the Raspberry Pi’s HDMI output onto the LED matrices.

Let’s get social

First, you need to get your Raspberry Pi all set up and talking to the social networks that you’re going to display. Usually, it’s advisable to install Raspbian without any graphical user interface (GUI) for most electronics projects, but in this case you’ll be actively using that GUI, so make sure you start with a fresh and up-to-date installation of full-fat Raspbian.

phpMyAdmin gives you an easy web interface to allow you to access and edit the device’s settings – for example, speed and direction of scrolling, API credentials, and the social network accounts to monitor

You start by turning your humble little Raspberry Pi into your very own mini web server, which will gather your credentials, talk to the social networks, and display the follower counts. To do this, you need to install a LAMP (Linux, Apache, MySQL, PHP) stack. Start by installing the Apache web server by opening a Terminal and typing:

sudo apt-get install apache2 -y

Then, open the web browser on your Pi and type http://localhost — you will see a default page telling you that Apache is working. The page on our little ‘website’ will use code written in the PHP language, so install that by returning to your Terminal and typing:

sudo apt-get install php -y

Once that’s complete, restart Apache:

sudo service apache2 restart

Next, you’ll install the database to store your credentials, settings, and the handles of the social accounts to track. This is done with the following command in your Terminal:

sudo apt-get install mysql-server php-mysql -y

To set a root password for your database, type the following command and follow the on-screen instructions:

sudo mysql_secure_installation

Restart Apache again. Then, for easier management of the database, I recommend installing phpMyAdmin:

sudo apt-get install phpMyAdmin -y

At this point, it’s a good idea to connect your Pi to a WiFi network, unless you’re going to be running a network cable to it. Either way, it’s useful to have SSH enabled and to know its IP address so we can access it remotely. Type the following to access Pi settings and enable SSH:

sudo raspi-config

To determine your Pi’s IP address (which will likely be something like 192.168.0.xxx), type either of the following two commands:

ifconfig # this gives you lots of extra info
hostname -I # this gives you less info, but all we need in this case

Now that SSH is enabled and you know the LAN IP address of the Pi, you can use PuTTY to connect to it from another computer for the rest of your work. The keyboard, mouse, and monitor can now be unplugged from the Raspberry Pi.

Social media monitor

To set up the database, type http://XXX/ phpmyadmin (where XXX is your Pi’s IP address) and log in as root with the password you set previously. Head to the User Accounts section and create a new user called socialCounter.

You can now download the first bit of code for this project by running this in your Terminal window:

cd /var/www/html

sudo apt-get update

sudo apt-get install git -y

sudo git clone https://github.com/paulfp/social- media-counter.git

Next, open up the db.php script and edit it to include the password you set when creating the socialCounter user:

cd ./social-media-counter

sudo nano db.php

The database, including tables and settings, is contained in the socialCounter.sql file; this can be imported either via the Terminal or via phpMyAdmin, then open up the credentials table. To retrieve the subscriber count, YouTube requires a Google API key, so go to console.cloud.google.com and create a new Project (call it anything you like). From the left-hand menu, select ‘APIs & Services’, followed by ‘Library’ and search for the YouTube Data API and enable it. Then go to the ‘Credentials’ tab and create an API key that you can then paste into the ‘googleApiKey’ database field.

Facebook requires you to create an app at developers.facebook.com, after which you can paste the details into the facebookAppId and facebookSecret fields. Unfortunately, due to recent scandals surrounding clandestine misuse of personal data on Facebook, you’ll need to submit your app for review and approval before it will work.

The ‘social_accounts’ table is where you enter the user names for the social networks you want to monitor, so replace those with your own and then open a new tab and navigate to http://XXX/socialmedia-counter. You should now see a black page with a tiny carousel showing the social media icons plus follower counts next to each one. The reason it’s so small is because it’s a 64×16 pixel portion of the screen that we’ll be displaying on our 64×16 LED boards.

GPIO pins to LED display

Now that you have your social network follower counts being grabbed and displayed, it’s time to get that to display on our screens. The first step is to wire them up with the DuPont jumper cables from the Raspberry Pi’s GPIO pins to the connection on the first board. This is quite fiddly, but there’s an excellent guide and diagram on GitHub within Henner Zeller’s library that we’ll be using later, so head to hsmag.cc/PLyRcK and refer to wiring.md.

The Raspberry Pi connects to the RGB LED screens with 14 jumper cables, and the screens are daisy-chained together with a ribbon cable

The second screen is daisy-chained to the first one with the ribbon cable, and the power connector that comes with the screens will plug into both panels. Once you’re done, your setup should look just like the picture on this page.

To display the Pi’s HDMI output on the LED screens, install Adafruit’s rpi-fb-matrix library (which in turn uses Henner Zeller’s library to address the panels) by typing the following commands:

sudo apt-get install -y build-essential libconfig++-dev

cd ~

git clone --recursive https://github.com/ adafruit/rpi-fb-matrix.git

cd rpi-fb-matrix

Next, you must define your wiring as regular. Type the following to edit the Makefile:

nano Makefile

Look for the HARDWARE_DESC= property and ensure the line looks like this: export HARDWARE_DESC=regular before saving and exiting nano. You’re now ready to compile the code, so type this and then sit back and watch the output:

make clean all

Once that’s done, there are a few more settings to change in the matrix configuration file, so open that up:

nano matrix.cfg

You need to make several changes in here, depending on your setup:

  • Change display_width to 64 and display_height to 16
  • Set panel_width to 32 and panel_height to 16
  • Set chain_length to 2
  • Set parallel_count to 1

The panel array should look like this:

panels = ( 
  ( { order = 1; rotate = 0; }, { order = 0; rotate = 0; } )
)

Uncomment the crop_origin = (0, 0) line to tell the tool that we don’t want to squish the entire display onto our screens, just an equivalent portion starting right in the top left of the display. Press CTRL+X, then Y, then ENTER to save and exit.

It ain’t pretty…but it’s out of sight. The Raspberry Pi plus the power supply for the screens fit nice and neatly behind the screens. I left each end open to allow airflow

Finally, before you can test the output, there are some other important settings you need to change first, so open up the Raspberry Pi’s boot configuration as follows:

sudo nano /boot/config.txt

First, disable the on-board sound (as it uses hardware that the screens rely on) by looking for the line that says dtparam=audio=on and changing it to off. Also, uncomment the line that says hdmi_force_hotplug=1, to ensure that an HDMI signal is still generated even with no HDMI monitor plugged in. Save and then reboot your Raspberry Pi.

Now run the program using the config you just set:

cd ~/rpi-fb-matrix

sudo ./rpi-fb-matrix matrix.cfg

You should now see the top 64×16 pixels of your Pi’s display represented on your RGB LED panels! This probably consists of the Raspberry Pi icon and the rest of the top portion of the display bar.

No screensaver!

At this point it’s important to ensure that there’s no screensaver or screen blanking enabled on the Pi, as you want this to display all the time. To disable screen blanking, first install the xscreensaver tool:

sudo apt-get install xscreensaver

That will add a screensaver option to the Pi’s GUI menus, allowing you to disable it completely. Finally, you need to tell the Raspberry Pi to do two things each time it loads:

  • Run the rpi-fb-matrix program (like we did manually just now)
  • Open the web browser in fullscreen (‘kiosk’ mode), pointed to the Social Counter web page

To do so, edit the Pi’s autostart configuration file:

sudo nano ~/.config/lxsession/LXDE-pi/autostart

Insert the following two lines at the end:

@sudo /home/pi/rpi-fb-matrix/rpi-fb-matrix /home/ pi/rpi-fb-matrix/matrix.cfg\

@chromium-browser --kiosk http://localhost/ social-media-counter

Et voilà!

Disconnect any keyboard, monitor, or mouse from the Pi and reboot it one more time. Once it’s started up again, you should have a fully working display cycling through each enabled social network, showing up-to-date follower counts for each.

It’s now time to make a surround to hold all the components together and allow you to wall-mount your display. The styling you go for is up to you — you could even go all out and design and 3D print a custom package.

The finished product, in pride of place on the wall of our office. Now I just need some more subscribers…!

For my surround, I went for the more rustic and homemade look, and used some spare bits of wood from an internal door frame lining. This worked really well due to the pre-cut recess. With a plywood back, you can screw everything together so that the wood holds the screens tightly enough to not require any extra fitting or gluing, making for easier future maintenance. To improve the look and readability of the display (as well as soften the light and reduce the brightness), you can use a reflective diffuser from an old broken LED TV if you can lay your hands on one from eBay or a TV repair shop, or just any other bit of translucent material. I found that two layers stapled on worked and looked great. Add some hooks to the back and — Bob’s your uncle — a finished, wall-mounted display!

Phew — that was quite an advanced build, but you now have a sophisticated display that can be used for any number of things and should delight your customers whilst helping to build your social following as well. Don’t forget to tweet us a picture of yours!

The post Build a social media follower counter appeared first on Raspberry Pi.

Hackspace magazine 9: tools, tools, tools

via Raspberry Pi

Rejoice! It’s time for a new issue of Hackspace magazine, packed with things for you to make, build, hack, and create!

raspberry pi press hackspace magazine

 

HackSpace magazine issue 9

Tools: they’re what separates humans from the apes! Whereas apes use whatever they find around them to get honey, pick pawpaws, and avoid prickly pears, we humans take the step of making things with which to make other things. That’s why in this issue of HackSpace magazine, we look at 50 essential tools to make you better at making (and by extension better at being a human). Take a look, decide which ones you need, and imagine the projects that will be possible with your shiny new stuff.

Konichiwakitty

In issue 9, we feature Konichiwakitty, known as Rachel Wong to her friends, who is taking the maker world by storm with her range of electronic wearables.

Alongside making wearables and researching stem cells, she also advocates for getting young people into crafting, including making their own wearables!

Helping

Remap is a fantastic organisation. It’s comprised of volunteer makers and builders who use their skills to adapt the world and build tech to help people with disabilities. Everyone in the maker community can do amazing stuff, and it’s wonderful that so many of you offer your time and skills for free to benefit people in need.

Music

The band Echo and the Bunnymen famously credited a drum machine as a band member, and with our tutorial, you too can build your own rhythm section using a Teensy microcontroller, a breadboard, and a few buttons.

And if that’s not enough electro beats for you, we’ve also got a guide to generating MIDI inputs with a joystick — because keyboards and frets are so passé.

Pi Wars

Having shiny new stuff on its own isn’t enough to spur most people to action. No, they need a reason to make, for example total mechanical dominance over their competitors. Offering an arena for such contests is the continuing mission of Tim Richardson, who along with Mike Horne created Pi Wars.

In its five-ish years, Pi Wars has become one of the biggest events on the UK maker calendar, with an inspired mix of robots, making, programming, and healthy competition. We caught up with Tim to find out how to make a maker event, what’s next for Pi Wars, and how to build a robot to beat the best.

Fame

Do you ever lie awake at night wondering how many strangers on the internet like you? If so (or if you have a business with a social media presence, which seems more likely), you might be interested in our tutorial for a social media follower counter.

raspberry pi press hackspace magazine

This build takes raw numbers from the internet’s shouting forums and turns them into physical validation, so you can watch your follower count increase in real time as you shout into the void about whether Football’s Coming Home. 

And there’s more…

In this issue, you can also:

  • See how to use the Google AIY Projects Vision kit to turn a humble water pistol into a single-minded dousing machine that doesn’t feel pity, fear, or remorse
  • Find out how to make chocolate in whatever shape you want
  • Learn from a maker who put 20 hours work into a project only to melt her PCBs and have to start all over again (spoiler alert: it all worked out in the end)

All this, plus a bunch of reviews and many, many more projects to dig into, in Hackspace magazine issue 9.

Get your copy of HackSpace magazine

If you like the sound of this month’s content, you can find HackSpace magazine in WHSmith, Tesco, Sainsbury’s, and independent newsagents in the UK. If you live in the US, check out your local Barnes & Noble, Fry’s, or Micro Center next week. We’re also shipping to stores in Australia, Hong Kong, Canada, Singapore, Belgium, and Brazil, so be sure to ask your local newsagent whether they’ll be getting HackSpace magazine. And if you’d rather try before you buy, you can always download the free PDF.

Subscribe now

Subscribe now” may not be subtle as a marketing message, but we really think you should. You’ll get the magazine early, plus a lovely physical paper copy, which has really good battery life.

raspberry pi press hackspace magazine

Oh, and 12-month print subscribers get an Adafruit Circuit Playground Express loaded with inputs and sensors and ready for your next project.

The post Hackspace magazine 9: tools, tools, tools appeared first on Raspberry Pi.