r/explainlikeimfive • • 1d ago

Engineering ELI5: How do boards like Raspberry Pi work?

I know that they are used for processes in all sorts of things but I don’t know how it works like where is the code written? How does that differ coding on something physical from coding for say a game engine? How is it used to make physical things work like machines?

77 Upvotes

44 comments sorted by

188

u/Hummerville 1d ago

Raspberry Pis are really just computers with Linux installed. You can write programs which cause input output pins to read or output logical signals. I use mine for my house alarm systems, Weather readings. Plex to play music etc.

86

u/OpulentGrouping973 1d ago

The GPIO pins are the whole game changer, regular computers can't just poke wires into the wall and sense voltage like that

Coding for a game engine is all virtual, you move a character and it's just math floating in memory. Coding for a Pi you write the same type of code but at the end you set a pin high and a relay clicks and a real light turns on somewhere in your house

It's weirdly satisfying the first time you make a physical thing respond to your code

39

u/SoulWager 1d ago

Normal processors, or their supporting chipset, do have GPIO pins, they're just not commonly broken out to a header on the motherboard. Though you can get motherboards that do, usually they're aimed at the industrial market.

20

u/mmn_slc 1d ago

One can add GPIO pins to a “regular” computer with a PCIe or USB adapter, or other means. 

28

u/LOSTandCONFUSEDinMAY 1d ago

You technically can do that with a regular computer with usb to GPIO modules but is way overkill and an SBC tends to be optimized for that kind of thing.

7

u/semi-proantfarmer 1d ago

Enhanced Parallel port had inputs and outputs pins. I remember in grade 11 making a 2x3 matrix with push buttons. and if you got the code right it turned on a green light and if you got it wrong it turned on a red light. I think it only had 8io pins that's why I did 2x3.

You can also buy serial connected input and output boards for your PC and program from there. I'm sure they make them usb now but ease of use serial is still the simplest.

1

u/htmlcoderexe 1d ago

i think you could do more than 6 with charlieplexing

6

u/thehandsomegenius 1d ago

It's fairly simple to add those pins to a desktop or laptop computer using a $6 microcontroller board.

You would usually only do that in development though. It's too expensive, bulky and power hungry for a finished product.

Back in the 80s and 90s, people used to run lots of industrial machinery on a 386 or a Commodore 64. A lot of those computers were in use for decades, because those very specialised systems are difficult to replace.

5

u/guantamanera 1d ago

You can add GPIO to a regular computer and sensors just like the pi. I chose not to because power consumption. Sometimes even the pi is too much and I use an 8 bitter MCU because I can run off a coin cell battery.

1

u/SoulWager 1d ago

IMO no reason to use an 8bit for a new project. They're often more expensive than a 32bit equivalent.

1

u/Salt_peanuts 1d ago

The comment you are replying to mentioned one reason- power consumption. Another reason might be size/weight. I’m sure there are others that I can’t think of. If there was no reason to use them… No one would bother to make them.

1

u/SoulWager 1d ago

32 bit processors are usually made on newer, smaller, lower power, process nodes. Can get 32 bit micros with sleep power ~1uA (suitable for a non-rechargeable coin cell).

A LOT of 8 bit microcontrollers are still made to support 30+ year old products with heavy safety or other reliability certification requirements.

The certification part is the expensive bit. Not worth doing if the old design still works, but those old designs are made on older equipment, that gets progressively more expensive to maintain, and which have fewer customers to spread the fixed costs over.

3

u/ave369 1d ago

There absolutely are x86 or 64 motherboards with GPIO pins. These are industrial motherboards for things like machine tools and diagnostic carts.

2

u/zgtc 1d ago

I mean, regular processors absolutely have GPIO pins, they’re just not exposed to the user.

Something like an AM4 Ryzen 7 has dozens of them, they’re just mixed in with the other thousand-plus pins.

3

u/deelowe 1d ago

GPIO isn't new or unique to rpi

2

u/MCWizardYT 1d ago

However, it is the advantage a pi has over an average desktop PC. That, and the size

1

u/Mynameismikek 1d ago

ISA slots were very similar to GPIO pins. You could write a bitmask to a particular address and it would set the appropriate pins high and low.

32

u/Salindurthas 1d ago

A Raspberry Pi is a small and relatively weak computer. It works in the same manner as any other computer.

If you ripped away the casing and keyboard and screen and mouse and so one, a normal computer is essentially just a big rasberry pi, and a raspberry pi is just a small computer.

The code is written and stored in the same way you can write code for a normal computer, but you might have to bring your own mouse&screen&keyboard (because the owner of the raspberry pi might not have a screen and keyboard habitually connected to it), or write it on your own larger and more comfortable computer and then transfer it over.

10

u/MasterGeekMX 1d ago

A Raspberry Pi is basically a PC but made with a phone chip. It uses a microSD card as it's hard drive, and it simply behaves like a regular computer: hook a keyboard, a mouse and a monitor, and you will see a desktop with icons and a taskbar. You can open up web browsers, write office documents, and do basically anything that a PC does. That works because the Raspberry Pi runs the Linux OS, which is an alternative to Windows and macOS. I myself use it on all my PCs, and I'm writing you from it right now.

To program it, you simply write code on it like on every computer out there. The key is that the Raspberry Pi has included inside it a series of pins, which can be configured as inputs or outputs. If set in output mode, you can tell the pin to get electrified or not, while in input mode the pin will check if something else electrifies it or not, and tell it to you. These pins are connected to the CPU inside the Pi, so all you need to work with it is some code that talks to it, which is done in the form of a code library that makes it easier.

Honestly I suggest getting one and playing it. They have a very cheap model called the Raspberry Pi Zero, which is meant to carry on lighter tasks as it only has half a gigabyte of RAM and a couple of cores. It won't open a web browser, but you can absolutely tinker with it. The Zero 2 simply makes it a bit tiny powerfull, and the W models add wifi and bluetooth onboard.

https://www.raspberrypi.com/products/raspberry-pi-zero/

https://www.raspberrypi.com/products/raspberry-pi-zero-2-w/

17

u/Public-Eagle6992 1d ago

A raspberry pi is just a small computer, it works the same as any other computer. How exactly any computer actually turns code into doing something is pretty complicated and someone else can surely explain that better than I can. A game engine is basically just a collection of already existing useful code so you don’t have to program everything from ground up again

14

u/Recurs1ve 1d ago

It's a small computer on a board. It has ram, cpu, sound and display all wired in. Most of them have some form of linux installed on them by default.

7

u/spookynutz 1d ago edited 1d ago

The code for the PI isn't fundamentally different than any other code. What separates the PI from something like a laptop or game console is GPIO (general purpose input/output). These are dedicated pins to allow inputting and outputting electrical signals.

For example, you could write a simple program that tells the PI to enable pin #3, which outputs 3 volts. If that pin is connected to an LED, then it will light up. If it's hooked up to a 120 volt relay, then it can control the power state of almost anything in your house. Lights, doors, fans, appliances, etc.

If you install a web server on the PI, and create a simple HTML page with a button to run that simple program, you can now control devices from anything with a web browser on your local network. If you set up port forwarding on your router, you can then control those devices from almost anywhere in the world. All from just being able to programmatically output 3 volts.

7

u/SeanAker 1d ago

It's really just a tiny computer. It works just like your desktop or laptop or phone does. It communicates with outside hardware the same way, by USB, etc.

3

u/heyitscory 1d ago edited 1d ago

It has small versions of all the things regular computers have. Programs (the code) are stored on SD cards, SSD drives, USB drives, network drives or any number of storage media, and it's run using the central processor.

The board has a lot of connectors that can be wired to machines to control their functions and take inputs from their sensors. Regular computers have that too. It's usually done with DB-9 serial connections but sometimes it uses USB, or lately, just through the network via Ethernet or wireless, but that seems slightly less magical than a computer with a robot arm connected to it.

3

u/aj10017 1d ago

It's a computer, but much smaller, and much slower. The data is stored on a Micro SD card most of the time. To interface with physical devices like motors or sensors, it has pins that can send and receive signals, which can be interpreted and controlled in software on the device.

2

u/davideogameman 1d ago

They are just cheaper cpus integrated with a bunch of io devices so you can plug in random hardware.  Nothing magically going on but you probably wouldn't want to spend $500 on a nice Intel laptop chip to control a strip of LEDs or a few motors and similar when a much cheaper chip will do.

Think of it like an e bike when a good pc is a Ferrari 

2

u/KenmoreToast 1d ago

A raspberry pi is just a small computer, using an operating system like Linux that doesn't use much memory.

Easier to strap a computer that's just a single small card into a robot than a whole laptop.

You can potentially run it like a traditional PC: plug it into a screen, mouse, and keyboard and do a lot of what you normally do on computers.

Or, you can get a version that only has enough features in the card/operating system to support running a single program, like controlling a robot.

2

u/BaggyHairyNips 1d ago

It's just a small computer. If you took the motherboard out of a desktop computer it would be basically the same thing. You can connect USB devices to it, connect monitor and keyboard, and all the normal computer things.

The one thing it has that a regular computer probably doesn't is a general purpose pin header (all those little single pins you see sticking out of the board).

You can write a program on the pi which controls those pins however you want. That makes it more flexible for being able to talk to other small devices or drive other small circuits for whatever project you want to do.

2

u/DBDude 1d ago

It’s a computer, that’s it. It has essentially the same stuff as in a desktop. You can buy one already loaded with an operating system and then download programs. As Or you can download an imager program that will put one of many versions of Linux on it already loaded with the programs and configurations for various purposes.

A Pi has a bunch of pins on it. Sending various commands will activate certain pins. You can use this to control things you hook up to it.

2

u/RyeonToast 1d ago

The code goes where code always goes. The computer needs a place for long-term data storage, and that's where the code goes. For Pi, it'll be an SD card inserted into the Pi. However, when it comes to computing, that really isn't much different than using some sort of hard drive in a desktop or laptop.

Writing code is writing code. The difference between a Pi and most desktops and laptops the processor is a different kind that accepts different instructions. That detail is mostly handled by the compiler program that turns the code you write into code the computer can read, but it means that code written for the Pi won't run as-is on your typical desktop.

Regarding physical machines, that's about the connectivity options. Both the Pi and a hypothetical desktop have ports for USB, video, and headphones, the Pi also has a set of pins on the side that connect to the rest of the computer. Apps can send signals or receive signals to or from those pins, kinda like they can to the USB or video ports. Those pins are pretty simple so they can be generally useful for a variety of systems, so they are sometimes referred to General Purpose Input/Output, or GPI/O.

2

u/semi-proantfarmer 1d ago

It's a small computer with decent speed and memory. It also has general purpose Input and Output pins. This lets you turn things on and off or know when let's say a button is pressed.

If you want to do this I suggest you buy an ESP32 to start. Find a tutorial online and start making projects. Arduino and Esp32 are easy entry level devices to start coding with.

2

u/BiomeWalker 1d ago

It's just a tiny computer with am easy access method for attaching other parts.

Code can be written on the Pi or elsewhere and loaded onto it just like any computer.

It doesn't different from code anywhere else.

As to the last question, the Pi has a set of what ate called General Purpose Input/Output pins (GPIO) which it can send current through or check of current is getting through which allow it to control motors or lights and get data from sensors.

That's all that the Pi has that makes it different from your desktop or laptop, the ability for you to easily add random parts.

2

u/Living_Fig_6386 1d ago

Raspberry Pi is a computer, like a smartphone but lose the touchscreen and add some USB ports and so on. They have a slot for a microUSD card that you can put software on. Most people use a version of Linux on them.

They’re just lightweight computers. You can use them for regular computer stuff. They’re not powerful like like your gaming rig, but they are practical for all sorts of things where a cheap computer would hit the spot.

1

u/ndiphilone 1d ago

A desktop/laptop computer is just a big Raspberry Pi, everything is same in terms of core computing hardware; it has a CPU, memory and some form of persistent storage, I/O ports

1

u/IAmJacksSemiColon 1d ago

The Raspberry Pi is a small ARM computer that runs Linux. The operating system is either flashed to an SD card or internal storage, and while Raspberry Pi OS is developed by The Raspberry Pi Foundation (based on Debian) you can run whatever code you want to on it.

There are a lot of replies about hardware, but to understand the culture around Raspberry Pi boards what you really need to understand is Free Open Source Software. FOSS is the ideal that you should be able to decide how to use your software, you should be allowed to see how it works, share copies with other people, and make your own changes to copies that you can share with others.

1

u/the_other_Scaevitas 1d ago edited 1d ago

Raspberry pis are just like any computer like your laptop or pc you have at home. They just removed everything that was unnecessary and made it as small as possible. You can still connect to it and see a desktop like any other PC.

Where is the code written? You can write it directly on the raspberry pi, but most people write it on their work computers, and then upload them to the pi. There are a few ways to do this, simple way is to just upload it to github, clone the repository on the pi and build it on the pi. Personally I use SFTP (secure file transfer protocol) to just send it directly to the pi

How does that differ coding on something physical from coding for say a game engine? There's no difference between coding for a raspberry pi and a game engine for a computer really. They use the same principles, same languages, though programming for a microcontroller (not a raspberry pi) or an FPGA can be a little different and these are what are commonly used to control a physical thing like machines. There are also PLAs, but I have no experience with them so I can't say.

How is it used to make physical things work like machines? Well it's a pretty complicated story. A raspberry pi has GPIO pins (General purpose input output pins). These pins can connect to your electronics to control them. You control how these GPIO pins work through calling special functions that writes values to special registers on the pi that controls the pins. The most basic example is that you can turn a GPIO pin on or off. So when it is on, it's outputing electricity, and you can use this to turn on an LED light. But you can also turn it on and off really quickly to make it look like the LED is dimmer than it actually is. This is called Pulse Width Modulation (PWM), and it is used to control a lot of different things, like for example a servo motor. The servo might be created so that 100 ms is a full 100 degree rotation, and so you turn on the GPIO pin for 10 ms. Now the servo motor will rotate (10 / 100)% = 10% of 360 degrees, so 36 degrees. Then there are a LOT of different communication protocols using these pins, like SPI, i2c, uart, etc. They are made by different people and companies, for example SPI was made by motorolla if I remember correctly. And different things have different forms of communication, for example a temperature sensor might communicate with i2c, so you plug in the corresponding wires with the sensor and write code to communicate with the temperature sensor using i2c

1

u/saschaleib 1d ago

A Raspberry Pi really isn’t that different from your PC or laptop: it has a processor, RAM, a card reader - and if needed you can connect an SSD drive. There are ports to connect keyboard and mouse, and of course a monitor.

The difference is that many parts are literally “scaled down” - not only in size, but also the processor isn’t nearly as powerful, the card drive is slower than your PCs SSD, it has less memory (well, unless you buy the 16GB version), etc.

That means you are somewhat limited with what software you can run - without a proper GPU you won’t have fun with bigger games, and as operating system there is either Linux or a much restricted version of Windows (don’t use that!).

But in the end, it is a computer like any other, and you can program it with the same or similar tools as any other.

1

u/binocular_gems 1d ago

Raspberry Pi's are just small, low-power computers that are similar to any other computer, a laptop, desktop, or even a tablet or a phone, though they're designed for hobbyists and tinkerers. They typically come pre-installed with a variant of Linux, and they operate like any other normal computer.

There's a line of hobbyists devices that rose to prominence around the same time as Raspberry Pi did, these are basic microcontrollers, things like Arduino or BeagleBoard. These are microcontrollers, not full computers, and they were popular with hobbyists and home engineering enthusiasts. WIth those, they don't run a complex operating system like how a Raspberry Pi does, they run (usually) a single computer program in a loop, you write that computer program on another computer, compile it, and then upload that to the board itself using a USB cable, and then when you plug the board in, it runs the program in a loop. These were popular for controlling electrical switches, so when you saw a lot of home Christmas light displays or simply robots, Arduino's were popular for that.

Coding an Arduino is different from coding for a game engine. You'd write your programs on another computer using the C programming language (or something that compiles to C / Embedded C), and Arduino provided an easy-to-understand API (basically a set of consistent and easy to understand interfaces for working with the board), and your program would be written to control switches on the board. Arduino provided a nice developer environment where you could simulate what your program was going to do so you could work on it on your machine, and then upload the final compiled program to the device.

There are ways to use a Raspberry Pi like an Arduino, but it's kind of overkill. Raspberry Pis are full computers, more like what you'd expect from a laptop or desktop computer, than a simple microcontroller. You could work with a game engine on a Raspberry Pi as long as it's powerful enough to run the software and has a build for that operating system. It wouldn't be really practical to use a game engine to program something for Arduino. You could, I'm sure that some tinkerers have Doom running on Arduino and playable via like, christmas tree lights or something crazy, I just don't know how practical that is outside of a parlor trick.

•

u/Deivedux 17h ago

As a dev, believe me, it's more complicated to support different operating systems than to support different processor architectures. The tools and compilers that we all use and depend on for turning code into binary make it near invisible for us, they're the ones do all the heavy lifting in correctly compiling into a correct set of binary instructions so that the program's end result is near identical on any processor. But it is our job to make sure we correctly handle every different operating system in their own ways.

•

u/Spooked_kitten 13h ago

Its a computer, but very small, not unlike a phone, it also has a lot of pins that you can have data come in and out, plug in an LED, figure out which pins need the right data to make the led blink, make a little program and off you go.

1

u/Dashing_McHandsome 1d ago

Probably most of the time a raspberry pi is running an operating system, which is Linux. So programming for it is quite a familiar experience for most programmers. You write code, save it to a micro sd card or some other storage device and then run it as you would run any other program.

To get your code to interact with the real world you make use of the GPIO header, or general purpose IO. These pins are connected to the CPU on the board and allow you to connect things like motors, screen, and sensors to your pi and control them through the code you write.

The environment the pi provides is in contrast with what would commonly be called a "true" embedded environment. This is the world in which developers tend to be a bit more specialized. The code written here runs without the help of an operating system and directly on a microcontroller. Usually the vendors of these chips provide an IDE to let the developer write code and upload it to some storage on the board they are working with. A lot of the time in this world the boards and electronics attached to it are bespoke creations for a specific product.

There's an interesting middle ground that has emerged over the years in this world and you can use devices like the Arduino or ESP32 to get some of the same sort of environment as a truly embedded device without needing to make your own boards and circuits. This is a great place for developers like myself who have tons of experience developing on an operating system, but don't have the intimate electronics knowledge that embedded development tends to demand.

I guess in short I would say getting down to this level is a different world than many developers ever experience, but there's no reason a seasoned developer couldn't pick up these concepts if they really needed to. I just like to poke around with some of this stuff as a hobby.

0

u/insertAlias 1d ago

I think your question fundamentally misunderstands some things. Raspberry PIs are just computers. Small, limited computers. They have a CPU, RAM, a network interface, USB ports, and everything else that you need to have a functional computer. It's just a relatively small amount of hardware horsepower. They have specific hardware to decode HD video, so it can output HDMI.

The code is written anywhere, like any other code. Or even directly on the Pi, if you have it running and SSH into it. Most people don't work like that though. They'd copy (via SSH/SCP) the built results to the Pi and run it there.

How does that differ coding on something physical from coding for say a game engine

The same way coding for anything else differs from coding for a game engine: you code to the SDK. In this case, you might not need any kind of special code, depending on what you're using the Pi for, but it also exposes "GPIO pins" (general purpose input/output) that are basically 5v pins you can target. With that, you can read voltages from external hardware like buttons, switches, potentiometers, and more, and you can send signals out too. This gets into electronic engineering though.