What programming language to use for programming pi

Thread Starter

fan1

Joined Jun 11, 2016
5
I am thinking of programming water pump with raspberry pi , so my question is what programming language should I use. I searched online and found i should use C or pyton. We did C in school so I know basic stuff and a little bit more(was working in code blocks). Python is completely secret for me. So what you guys suggest? All i need to program is a simple on/off loop with a timer.
 

tindel

Joined Sep 16, 2012
936
I wouldn't waste the space or horsepower of a pi to run a pump timer. I'd use a micro with a 32kHz crystal. This will limit you to C or asm. I'd suggest C because it's pretty much universally used in embedded systems. Check out the MSP430 launchpad series. I'd suggest this one because it will hold data in FRAM even thru a power cycle, has a super cap to keep the RTC time correct when unplugged (assuming you've optimized for low power), and has a 32kHz crystal on board. Everything you need for a good timer application.
 

hellifino

Joined Jul 2, 2015
19
Just grab an Arduino nano off Amazon for a few bucks. You'll prolly need to pick up a FTDI converter too (uses a MINI usb cable)... Install Arduino IDE - it's all C. You'll also need to build a driver circuit for your pump since that's going to pull more current than an Arduino can source (You can also buy these off Amazon - search "motor driver shield" etc).
 

mcgyvr

Joined Oct 15, 2009
5,394
I've been having fun with ESP8266 micros lately.. Super simple/cheap to connect to wifi and get the time from a time server then perform functions off that..
It can be programmed via C or using the Arduino IDE (Arduino/wiring language)
 

GopherT

Joined Nov 23, 2012
8,009
I've been having fun with ESP8266 micros lately.. Super simple/cheap to connect to wifi and get the time from a time server then perform functions off that..
It can be programmed via C or using the Arduino IDE (Arduino/wiring language)
Can you share a simple program for your ESP8266 using the arduino IDE? I'm not having much luck using the arduino IDE.
 

mcgyvr

Joined Oct 15, 2009
5,394
It works with all ESP8266 boards (probably).. I know it works with just the cheapo $1.99 "base" ESP8266 modules that you need the FTDI board/regulators,etc.. to program..

I've just been using the huzzah but I'm fairly sure you need to put the ESP8266 barebones chips into "bootloader" (pull some pins high/low) mode too so they can be programmed..
 

vpoko

Joined Jan 5, 2012
267
If you decide to use a Pi (which I agree is extreme overkill for what you're doing), you can use any language you like. There are memory locations that you can write to/read from that correspond to the GPIO pins, and since the Pi gives you a full Linux environment (or in theory other OS's, though Linux is what people use), you can use any language supported on that platform. C, C++, Python, Java, C# (Mono), etc. You can also create simple drivers for the GPIO in one language and use the driver from another.
 

Thread Starter

fan1

Joined Jun 11, 2016
5
Thanks guys for all the help. I didn't realise pi might be an overkill. I might grab something else. Can you tell me what would be best use for pi.
 

GopherT

Joined Nov 23, 2012
8,009
Thanks guys for all the help. I didn't realise pi might be an overkill. I might grab something else. Can you tell me what would be best use for pi.
Overkill or not, if you have a pi and you have one value-added project idea, use the pi.
 

vpoko

Joined Jan 5, 2012
267
Anything where you'd benefit from having a full OS and relatively strong hardware (1.2 GHz CPU and 1 GB RAM on the Raspberry Pi 3). Applications that use TCP/IP networking, need a filesystem, need the ability to play/record/transcode audio or video, need to process large amounts of data, want to present a full GUI, or where you'd benefit from multitasking might be some examples, but of course there are countless other uses for it. I suppose I think of it more like a little PC (that can be put into a solution if need be) than an actual piece of embedded electronics like an Atmel AVR chip. It's not that it's wrong for your application, but I guess it gives people some kind of uneasy feeling to use an over-engineered solution when a lower-cost, lower-power one would work just as well. At $35 for the Raspberry Pi, the feeling might not be all that justified, and it's a pretty cool piece of hardware to have and learn to use. And if you already have it, there's definitely sense in using it (though it might feel underappreciated controlling a pump when it's capable of so much more).
 

MPep

Joined Dec 24, 2014
2
Just a suggestion but for a simple programmable timer loop, check out PICAXE. All programming is done using BASIC. Easily readable and they have their own friendly forum.
 

odm4286

Joined Sep 20, 2009
265
Here's my two cents about Python vs C. Learn C, Python might be "easier" but it'll hold your hand too much. I also believe Python will teach you some bad habits. I don't like how Python handles datatypes, there aren't any, and the fact that it depends on indentation vs brackets and semicolons.



If you decide to use a Pi (which I agree is extreme overkill for what you're doing), you can use any language you like. There are memory locations that you can write to/read from that correspond to the GPIO pins, and since the Pi gives you a full Linux environment (or in theory other OS's, though Linux is what people use), you can use any language supported on that platform. C, C++, Python, Java, C# (Mono), etc. You can also create simple drivers for the GPIO in one language and use the driver from another.
Not to hijack the thread but is there a way to write/read a register on the Pi to handle GPIO? I looked into it briefly and from what I saw it wasn't possible to access those memory locations as you would on a PIC MCU for example.
 

nsaspook

Joined Aug 27, 2009
13,308
Not to hijack the thread but is there a way to write/read a register on the Pi to handle GPIO? I looked into it briefly and from what I saw it wasn't possible to access those memory locations as you would on a PIC MCU for example.
Normally due to memory protection you can't directly write/read chip level registers on a RPi running Linux unless the program is running with kernel privileges. Nothing stops you from running 'bare metal' sans OS on the hardware just like a PIC MCU if that's your fancy but it's a complex task just to get beyond 'blink led'.

http://www.valvers.com/open-software/raspberry-pi/step01-bare-metal-programming-in-cpt1/
 

odm4286

Joined Sep 20, 2009
265
Normally due to memory protection you can't directly write/read chip level registers on a RPi running Linux unless the program is running with kernel privileges. Nothing stops you from running 'bare metal' sans OS on the hardware just like a PIC MCU if that's your fancy but it's a complex task just to get beyond 'blink led'.

http://www.valvers.com/open-software/raspberry-pi/step01-bare-metal-programming-in-cpt1/
Interesting, feel free to tell me if the following question belongs in its own thread. How is it the Python libraries are able to write/read to these registers?
 

nsaspook

Joined Aug 27, 2009
13,308
Last edited:

hellifino

Joined Jul 2, 2015
19
Here's my two cents about Python vs C. Learn C, Python might be "easier" but it'll hold your hand too much. I also believe Python will teach you some bad habits. I don't like how Python handles datatypes, there aren't any, and the fact that it depends on indentation vs brackets and semicolons.





Not to hijack the thread but is there a way to write/read a register on the Pi to handle GPIO? I looked into it briefly and from what I saw it wasn't possible to access those memory locations as you would on a PIC MCU for example.
Normally due to memory protection you can't directly write/read chip level registers on a RPi running Linux unless the program is running with kernel privileges. Nothing stops you from running 'bare metal' sans OS on the hardware just like a PIC MCU if that's your fancy but it's a complex task just to get beyond 'blink led'.

http://www.valvers.com/open-software/raspberry-pi/step01-bare-metal-programming-in-cpt1/
Yes you can write directly to the GPIO registers and do all the bit banging you want by writing to the memory. Addresses are different for the different versions of the Pi. Just google "Raspberry Pi GPIO register address" and you should find it. Plenty of examples.

No idea where @nsaspook is pulling that from. When you write to those addresses, you are directly controlling the hardware, no linux kernel involved. I've never had to do anything other than write to the memory address any RPi (don't have a pre-zero and 3 yet though).
 

nsaspook

Joined Aug 27, 2009
13,308
Yes you can write directly to the GPIO registers and do all the bit banging you want by writing to the memory. Addresses are different for the different versions of the Pi. Just google "Raspberry Pi GPIO register address" and you should find it. Plenty of examples.

No idea where @nsaspook is pulling that from. When you write to those addresses, you are directly controlling the hardware, no linux kernel involved. I've never had to do anything other than write to the memory address any RPi (don't have a pre-zero and 3 yet though).
You can directly write GPIO physical addresses in bare metal supervisor mode as I said in my reply. With an OS like Linux with memory protection normally you would ioremap the GPIO address into kernel space via the MMU so you don't need to define an exact addresses in programs only offsets from the returned address so the code will run on different versions of the RPi without modification.

https://raw.githubusercontent.com/nsaspook/daq_gert/master/daq_gert.c
Code:
/* Use the kernel system_rev EXPORT_SYMBOL */
devpriv->RPisys_rev = system_rev; /* what board are we running on? */
if (devpriv->RPisys_rev < 2) {
dev_err(dev->class_dev, "invalid RPi board revision! %u\n",
devpriv->RPisys_rev);
return -EINVAL;
}

dev->iobase = GPIO_BASE; /* bcm iobase */
/*
* dev->mmio is a void pointer with 8bit pointer indexing,
* we need 32bit indexing so mmio is casted to a (__iomem uint32_t*)
* pointer for GPIO R/W operations
*/
dev->mmio = ioremap(dev->iobase, SZ_16K);
if (!dev->mmio) {
dev_err(dev->class_dev, "invalid gpio io base address!\n");
return -EINVAL;
}

devpriv->timer_1mhz = ioremap(ST_BASE, 8);
if (!devpriv->timer_1mhz) {
dev_err(dev->class_dev, "invalid 1mhz timer base address!\n");
return -EINVAL;
}
 
Last edited:

hellifino

Joined Jul 2, 2015
19
You can directly write GPIO physical addresses in bare metal supervisor mode as I said in my reply. With an OS like Linux with memory protection normally you would ioremap the GPIO address into kernel space via the MMU so you don't need to define an exact addresses in programs only offsets from the returned address so the code will run on different versions of the RPi without modification.

https://raw.githubusercontent.com/nsaspook/daq_gert/master/daq_gert.c
Code:
dev->iobase = GPIO_BASE; /* bcm iobase */
/*
* dev->mmio is a void pointer with 8bit pointer indexing,
* we need 32bit indexing so mmio is casted to a (__iomem uint32_t*)
* pointer for GPIO R/W operations
*/
dev->mmio = ioremap(dev->iobase, SZ_16K);
if (!dev->mmio) {
dev_err(dev->class_dev, "invalid gpio io base address!\n");
return -EINVAL;
}

devpriv->timer_1mhz = ioremap(ST_BASE, 8);
if (!devpriv->timer_1mhz) {
dev_err(dev->class_dev, "invalid 1mhz timer base address!\n");
return -EINVAL;
}
Actually your post wanted him to use a certain OS type. Point is, regardless of the OS (not that the OS has a choice), it is possible and not difficult. On every OS that runs on the Pi.
 
Top