Raspberry Pi programming in C - Page 1
Forum section: electronics / projects

author: ricko_uk
author_rank: Super Contributor
timestamp: March 21, 2025, 11:40:19 am
content: Hi,I have programmed various micros quite a bit in C but never before used the Raspberry Pi. A client is asking to potentially implement a system using a Raspberry Pi (it doesn't matter which one) driving this touch display:https://www.waveshare.com/3.4inch-dsi-lcd-c.htmI was wondering how easy and straight forward it is to develop simple application with multiple screens with only buttons (to turn on/off various devices)specifically using C ?Assuming someone knows C quite well (C++ a bit rusty), but never used the Raspberry Pi platform or any of the development environments / libraries etc required to make such simple application, how long would it take them to get from 0 to up and running with a couple of screens and a couple of buttons? Just as a rough idea tog et a feel for it... A day? A week? A month?Is the learning curve steep? Any things I should be aware of from the beginning before making such decision?Thank you

author: elektryk
author_rank: Regular Contributor
timestamp: March 21, 2025, 11:54:20 am
content: Doing it from scratch may be difficult, but from what I see they provide install script, so driver part should be already done.Then you can do UI part in whatever you want, e.g. GTK (c) or QT (cpp).

author: squadchannel
author_rank: Frequent Contributor
timestamp: March 21, 2025, 12:02:04 pm
content: you know C, probably use GTK, but using a Linux-based system with an SD card, such as RaspberryPi, I think the firmware would be frequently corrupted.If it were me, use STM32 and TouchGFX.(or LVGL? etc...)

author: pcprogrammer
author_rank: Super Contributor
timestamp: March 21, 2025, 12:09:08 pm
content: Depends on how good your linux knowledge is.The easiest way to use the Raspberry PI is to load a linux system onto it. Display and touch panel support are included and programming it in C is as simple as C.There are lots of examples on how to control IO with the Raspberry PI.I used minibian, with a headless setup, to develop my central heating control system with all the extra code needed written in C. With the compiler on the Raspberry PI and connecting via ssh it was just as easy as writing C for desktop linux. Unfortunately minibian is no longer supported, but raspbian is still there and can be used for your purpose.It did not take me that long to get it up and running, but already knew some ins and out on linux. No expert at the time though, and even now, after working with linux and solving issues for 10 years or so, still not a real linux expert.The display you linked to will most likely work out of the box, by setting the correct device tree for it. The wiki page for it provides al the information you need.The SD card corruption mentioned by squadchannel is only a problem when you store data on the card regularly and switch of the Pi during a write. With proper power down or a newer version with the usage of eMMC it should not be a problem.My system used a harddisk because it was logging temperatures in a database and that is definitely sensitive when not properly powered down.For the graphicsLVGLis probably a better choice than the GTK or QT mentioned by others.

author: elektryk
author_rank: Regular Contributor
timestamp: March 21, 2025, 12:27:48 pm
content: LVGL is also good idea, because its pretty portable, so you can use (almost) the same code with bare metal and OS.

author: bostonman
author_rank: Super Contributor
timestamp: March 21, 2025, 01:14:01 pm
content: Does a difference exist between programming a Pi and Arduino in C and straight C?I know some basic C and have been tinkering with a Arduino Nano, and the example programs look like generic C.

author: elektryk
author_rank: Regular Contributor
timestamp: March 21, 2025, 01:43:54 pm
content: Yeah, Arduino users often doesn't use all of the cpp features, so sometimes code may look like plain c.BTW some cpp features aren't advisable for embedded systems.

author: SiliconWizard
author_rank: Super Contributor
timestamp: March 21, 2025, 01:57:28 pm
content: Well unless you client is ok with the Pi being programmed "bare metal" (which is doable but not officially supported), you're going to be dealing with a Linux OS on the Pi, so after that, as long as the display is going to be a HDMI or MIPI DSI (like here) one, it's going to be seen as a regular display for the OS and you'll be able to use pretty much any GUI library available on Linux. If the language is C only, the obvious toolkit is GTK (3/4) which is one of the few that have a direct C API.LVGL is an interesting suggestion as the time you'll invest in it will be reusable for other projects on more modest targets such as MCUs, so if you have potential projects on MCUs with a GUI, then I would recommend it over GTK, that'll be time better invested.Just a note regarding this display: while it should be usable on a Pi via its MIPI DSI interface, you'll have some configuration work to do for that on the Pi side, and its odd resolution and shape may give you some headaches, but that's probably doable. I wanted to point this out, but as for help, I'll let others who have already done this chime in.

author: pcprogrammer
author_rank: Super Contributor
timestamp: March 21, 2025, 02:53:59 pm
content: Quote from: SiliconWizard on March 21, 2025, 01:57:28 pmDon't forget about X11. I use it to make my GUI's on the desktop. Rather straight forward and in plain C.

author: Laval
author_rank: Regular Contributor
timestamp: March 21, 2025, 03:31:08 pm
content: Quote from: bostonman on March 21, 2025, 01:14:01 pmAs far as the language is concerned, no. C is basically the same, it's a fairly simple and basic language and it doesn't do much in and by itself unless you want to go bare metal. The differences come with the frameworks and external (external to the language) libraries you will use. Such frameworks are used to deal with GUI (Gtk, Qt, wxWidget, LVGL, etc) as well as networking (http clients, usb communication, ...) and other higher level tasks encountered when developing applications. These are not part of the language. C is a bare bone language, it has basic types for variables, arrays and structs, it has flow control instructions like conditions and loops as well as basic io and maths libraries. Learning all these external frameworks can have a steep learning curve. A Pi is a computer that uses an OS and you are basically developing an application.You mentioned C++. C++ is a superset of C. It is like C but with added features to support object oriented programming. It also has a more comprehensive standard library that comes with it. OOP is nothing more then a way to organized you code. It doesn't solve much problem at least at the algorithmic level, it's really just a different way to think and structure your code. (A lot will argue OPP creates its share of problems and there are valid arguments for this, this is a debate I'm not going to enter.) There is also a learning curve associated to OOP. Like C though, C++ needs to use external libraries for GUI, network and many other application related things.Programming a Pi means entering more into the realm of software and application development.

author: ricko_uk
author_rank: Super Contributor
timestamp: March 23, 2025, 10:04:06 am
content: Thank you all for the information and the suggested options. Very useful.

author: bostonman
author_rank: Super Contributor
timestamp: March 23, 2025, 01:38:42 pm
content: Due to a hardware project that involves a Nano, I've been using Arduino software that allows me to take the program developed by someone and program the Nano.A few line edits need to be made, and, with my limited knowledge of C, I'm able to dive into the code to make the changes.The reason I asked about the differences in C and a Nano is liking the Arduino software. I think the Arduino software is nice and user friendly. I can edit the C program and compile it all within the same window. In the past I've used Visual Studio along with other pieces of software. With VS, I've entered working C++ programs I've saved from when I took a course decades ago and it always gives compiling errors. Same with some basic C programs.What I found (or believe was the case) was VS needed settings tinkered to properly compile and I found myself tinkering with VS more than I got to learn C. Other programs that were less involved like GTK (or something like that I think) result in too much window switching and manually typing the file name to run; or the program quickly opens a DOS window and then closes without me seeing the program run.With Arduino, everything seems to be all in one program, but wondered whether it is ideal software for learning C (or expanding my C knowledge).

author: Laval
author_rank: Regular Contributor
timestamp: March 23, 2025, 04:25:52 pm
content: Quote from: bostonman on March 23, 2025, 01:38:42 pmVisual Studio is an IDE (Integrated Development Environment) which integrates a compiler, a linker, a debugger and an enormous amount of libraries. Without going into the specifics of the problems you encountered, C and C++ programs will typically use libraries because those languages don't do a lot of things out of the box e.g. GUI, Network, etc. If you take a program that has been written in C that makes calls to some library functions, you need to have those libraries integrated in your project to be able to call them. They are not part of the language. You can't just copy an Arduino program, paste it in a Visual Studio command line program and expect it to work.As for learning and expanding you knowledge of C itself, yes Arduino is as good a platform as any but all platforms have their specifics and they are different. Another thing that helps a lot with native programming is learning how toolchains work, what is a compiler, a linker and libraries and how they are used to generate executable code from text.

author: zapta
author_rank: Super Contributor
timestamp: March 23, 2025, 08:44:27 pm
content: May be easier to write it in Python.For examplehttps://projects.raspberrypi.org/en/projects/getting-started-with-guis/5Use a good AI such as chatgpt or grok and they will help you.

author: Laval
author_rank: Regular Contributor
timestamp: March 23, 2025, 11:27:22 pm
content: Quote from: zapta on March 23, 2025, 08:44:27 pmThat's certainly an alternative. Keep in mind that using an interpreter like Python consumes more resources like memory which small MCUs have little of. This may or may not be a problem depending on what your program does.

author: radiolistener
author_rank: Super Contributor
timestamp: March 23, 2025, 11:41:27 pm
content: you can useImGui frameworkwith SDL2 OpenGL ES backend to implement GUI with hardware accelerated graphics, it can run on Wayland or just KMSDRM mode which can be used with light version of linux with no X11/Wayland desktop. Raspberry Pi Zero 2W is enough to run pretty complicated GUI with realtime rendering.Here is example how ImGui looks:It supports touch, but I didn't used it. There are different bindings for ImGui, so you can use it with c++, c#, python and other programming languages.Regarding to the system, Wayland desktop is better, because it is more lightweight and has much more faster and smooth graphics than X11. The performance difference between X11 and Wayland is about 30%. Wayland is even a little bit better than KMSDRM backend for OpenGL ES apps - the difference is not significant, but running app under Wayland give you a little bit better frames per second.Raspberry Pi 4 GPU is able to run Quake3 or Arx Fatalis 3D games with nice pretty playable fps, when running them on Wayland backend you will not notice lags, so it will be enough for pretty complex GUI.

author: ledtester
author_rank: Super Contributor
timestamp: March 23, 2025, 11:53:14 pm
content: Quote from: zapta on March 23, 2025, 08:44:27 pmI think using Python is definitely a great way to learn a GUI library and it may also be suitable for your application since you are developing on a Raspberry Pi.For lvgl there isn't a Python binding as far as I can tell but there is a MicroPython binding:https://github.com/lvgl/lv_micropythonand MicroPython is usable on more capable microcontrollers if you are not on a full-fledged Linux system.

author: dobsonr741
author_rank: Frequent Contributor
timestamp: March 24, 2025, 02:35:06 am
content: Bare metal is simple. Just use Circle:https://github.com/rsta2/circleit’s awesome. Use ChatGPT if your C++ is rusty. Or Cursor, if you can afford subscribing. Best monthly subscription I ever had.

author: ricko_uk
author_rank: Super Contributor
timestamp: March 24, 2025, 03:14:24 am
content: Thank you for the additional info. A lot to look into.

author: radiolistener
author_rank: Super Contributor
timestamp: March 24, 2025, 11:53:40 am
content: Quote from: dobsonr741 on March 24, 2025, 02:35:06 amUsing Linux it is pretty simple to write OpenGL apps, for example, here is simple OpenGL code which renders triangle:Code:#include <SDL2/SDL.h>#include <GL/gl.h>#include <stdio.h>float vertices[] = {// Positions // Colors (RGB)0.0f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // Top (Red)-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // Bottom Left (Green)0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f // Bottom Right (Blue)};void renderTriangle() {glClearColor(0.2f, 0.3f, 0.3f, 1.0f);glClear(GL_COLOR_BUFFER_BIT);glEnableClientState(GL_VERTEX_ARRAY);glEnableClientState(GL_COLOR_ARRAY);glVertexPointer(3, GL_FLOAT, 6 * sizeof(float), vertices);glColorPointer(3, GL_FLOAT, 6 * sizeof(float), vertices + 3);glDrawArrays(GL_TRIANGLES, 0, 3);glDisableClientState(GL_COLOR_ARRAY);glDisableClientState(GL_VERTEX_ARRAY);}int main() {if (SDL_Init(SDL_INIT_VIDEO) < 0) {printf("SDL_Init() failed: %s\n", SDL_GetError());return -1;}SDL_Window* window = SDL_CreateWindow("OpenGL Triangle", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_OPENGL);if (!window) {printf("SDL_CreateWindow() failed: %s\n", SDL_GetError());return -1;}SDL_GL_CreateContext(window);SDL_Event event;int running = 1;while (running) {while (SDL_PollEvent(&event)) {if (event.type == SDL_QUIT) running = 0;}renderTriangle();SDL_GL_SwapWindow(window);}SDL_DestroyWindow(window);SDL_Quit();return 0;}Then you can run it using X11, Wayland, or using lightweight Linux with no graphical desktop manager (using its KMSDRM mode). It will use full GPU acceleration.Is it really more simple to use bare metal? Can you show more simple code to do the same?

author: SiliconWizard
author_rank: Super Contributor
timestamp: March 24, 2025, 12:31:13 pm
content: Quote from: dobsonr741 on March 24, 2025, 02:35:06 amCircle is fantastic work, but it's C++ and the OP said "specifically C". And, as I mentioned, while baremetal works and Circle is great, this is an approach that is based on a lot of reverse engineering and the client may not be comfortable with that.Certainly something to consider otherwise and if you're not allergic to C++.In a similar vein, but for those who aren't against trying something else and use Pascal, there is Ultibo:https://ultibo.org/very interesting stuff. I don't know how active the project still is though, it doesn't seem to have RPi 5 support yet and may not have seen a lot of activity lately. Definitely outside of what the OP is looking for, but something that could pique some people's curiosity.

author: DonKu
author_rank: Regular Contributor
timestamp: March 24, 2025, 02:01:05 pm
content: Quote from: pcprogrammer on March 21, 2025, 12:09:08 pmYou can alsoinstall FreeBSD on a RPi. My RPi with FreeBSD on a SD runs reliably regardless of the many times over the years its power plug was pulled without a graceful shutdown.

author: Kasper
author_rank: Frequent Contributor
timestamp: March 24, 2025, 04:21:56 pm
content: Quote from: SiliconWizard on March 24, 2025, 12:31:13 pmYears ago I made a trail camera prototype with RPi. Not knowing RPi, I started with Python following beginner guides. Couldn't believe how easy it was to get a camera running and saving pictures.Then to get it to boot faster, I tried Ultibo and Pascal. That was at least 10x more work. I think I got it down to 3 seconds from power on to picture taken. Not fast enough for a trail camera but a good learning experience.

author: bostonman
author_rank: Super Contributor
timestamp: March 24, 2025, 10:26:25 pm
content: It seems the Arduino may be good for C programming, but I don't see the ability to create an EXE from it.The thing I like about this: you have the C program, click compile, and it complies. The way I've done it which I think is a pain is run gnn or something like that, blah blah.This isn't the first time I've mentioned searching for a simple GUI to do C, but it's often deviated towards suggestions about doing something in C++ or Python, etc...Ideally I'd like a simple GUI based program on the idea of VS that allows me to compile with a few mouse clicks and run an EXE but without having to dive into endless options that cause me to wonder if my C program is wrong or I need to tweak an option.

author: abeyer
author_rank: Frequent Contributor
timestamp: March 25, 2025, 01:51:05 am
content: Quote from: bostonman on March 24, 2025, 10:26:25 pmIf someone had a simple gui that let people know that their C programs were correct they'd be way too rich to be commenting here.

author: radiolistener
author_rank: Super Contributor
timestamp: March 25, 2025, 02:00:00 am
content: There is no difference for C, C++, C#, Python or other languages, they all are supported on Raspberry Pi.So you don't have to worry about it, just use the language you like best.Quote from: bostonman on March 24, 2025, 10:26:25 pmJust install Geany editor, open some *.c file and open menu Build->Set Build Commands and fill this command in the Execute field:Code:gcc "%f" -o "%e" -Wall -lGL -lSDL2 && "./%e"Now you can compile and run the program with simple pressing button with paper airplane ("Run or view the current file"). It will open terminal window with compilation results and if compilation is ok, your app will be started.If you need to link some library, just all -l option to the list, the same as it done for -lGL

author: bostonman
author_rank: Super Contributor
timestamp: March 25, 2025, 04:00:22 am
content: This looks like it has potential.I assume you mean add the gcc "%f" -o "%e" -Wall -lGL -lSDL2 && "./%e" as I did?Where do I add the library?Not sure if I needed the library, but I tried a hello world program without success.

author: pcprogrammer
author_rank: Super Contributor
timestamp: March 25, 2025, 06:31:27 am
content: Quote from: bostonman on March 24, 2025, 10:26:25 pmBest to get your terminology right. Arduino is a company that delivers open source hard and software. The Arduino IDE is the integrated development environment they provide to allow people to easily develop programs for the boards supported by this Arduino IDE.I have not tried it, but I think it will be possible to create an executable with it, because under the hood it makes use of compilers and libraries. It won't be easy, but still.Quote from: bostonman on March 24, 2025, 10:26:25 pmIf you are looking for ways to easily create programs in C that run on your desktop, there are many IDE's that can help you with it. For Windows there used to be and probably still is, the paid version of Visual Studio, with a build in GUI designer. But the downside is that for GUI based programs they force you onto C++. It does allow you to make command line based programs in plain C though.On Linux there are options to create GUI based programs in plain C, but you do have to setup your IDE project to include the right libraries and select the correct compiler for the job. There is no do it all for you solution to magically know what it is that you want and setup everything for you. Even if one uses an AI to do the work for them, one has to know what to ask the AI to do, and also verify if the response is correct.Programming means doing the work and one has to learn to do this.

author: radiolistener
author_rank: Super Contributor
timestamp: March 25, 2025, 11:33:07 am
content: Quote from: bostonman on March 25, 2025, 04:00:22 amIn this command -lGL means that compiler needs to link GL library.-lSDL2 means that compiler needs to link SDL2 library.The same you can link other libraries.For hello world example you don't need libraries, so you can use this command: gcc "%f" -o "%e" -Wall && "./%e"I added -lGL -lSDL2 options for my previous OpenGL example which needs OpenGL and SDL2 libraries.Quote from: bostonman on March 25, 2025, 04:00:22 amyes, now click on the paper airplane button when editing your *.c file and it will open Linux terminal window:I recommend to install gnome-terminal:Code:sudo apt install gnome-terminalWhen installed, Geany will launch gnome-terminal by default. It looks better.Now you can try to run in that way myprevious example. But before duing it, you need to make sure that SDL2 library is installed:Code:sudo apt install libsdl2-devNow click on Run button and you will see this result:PS: in these examples I assume you're doing it on Linux in Raspberry Pi OS. Your screenshot looks like Windows XP. Programming on Windows is less user friendly and needs to install and configure compiler, it will be complicated for newbie. And since you're planning to write program for Raspberry Pi, Windows is a wrong choice as development platform, because Raspberry Pi OS is Linux.

author: bostonman
author_rank: Super Contributor
timestamp: March 26, 2025, 12:59:58 am
content: I wasn't planning to program for the PI exclusively or an Arduino, I meant generally speaking, I'd like a simple GUI based editor/compiler to create C programs in a Windows OS; not Linux.

author: radiolistener
author_rank: Super Contributor
timestamp: March 26, 2025, 01:00:13 pm
content: Quote from: bostonman on March 26, 2025, 12:59:58 amIn any case, if you're planning to develop and debug software for Raspberry Pi, you're needs to do it on Linux.If you want to develop GUI app for Windows XP, it's better to use C# language with .net 4 framework and WinForms. C# is the king for Windows world for GUI apps, it allows to easy develop and debug GUI apps. But notice, that .net 4.0 is the latest framework which is supported on Windows XP. Regarding to WinForms - its more ancient than WPF and other GUI frameworks, but it is much more easier for cross-platform apps, you can run WinForms apps on Linux and MacOS.