I'm have a 16x2 LCD button shield for RPI similar to Adafruits. It works using the provided Python library so the setup is ok.

The problem is I can't find any working C code or library to work with this setup. I've tried Gordons tutorial and some other sources I found but with no luck yet.

Has anyone got any working solution for this?

P.S. RPI is running on ArchLinux

1 Answer 1


I have found a workaround for this issue. Since I could not migrate the solution python I needed to get it working under C. The solution is to use Python-C API which lets you call python calls and get results.

Most of the information I found in this codeproject post

I created simple methods for my needings which included some "wait for a button press" method and such. For example if I have a method to print on a screen:


from time import sleep
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
import sys
btnsleepcycle = 0.01

class lcdSlave():

  def __init__(self):
      self.lcd = Adafruit_CharLCDPlate()
      self.lcd.begin(16, 2)

[Some code ommited]

  def lcdprint(self, message):
      return 0

[More code ommited]

Now when I want to call the code from my C code I must use the Python-C API call and make an include.

#include "python.h"

The rest is now on your C coding skills but for the same lcd print it is as easy as:

#include <Python.h>
int main(int argc, char *argv[])
    int result = 0;
 PyObject *pName, *pModule, *pDict,
             *pClass, *pInstance, *pValue, *pMessage;
// Load the module object
    pName = PyString_FromString("lcdSlave");
    pModule = PyImport_Import(pName);
// pDict is a borrowed reference
    pDict = PyModule_GetDict(pModule);
    pClass = PyDict_GetItemString(pDict, "lcdSlave");
    // Create an instance of the class
    if (PyCallable_Check(pClass))
        pInstance = PyObject_CallObject(pClass, NULL);
    pName = PyString_FromString("lcdprint");
    char* msg = "Your message\nhere"; //  '\n' will go straight to 2nd line
    pMessage = PyString_FromString(msg);
    pValue = PyObject_CallMethodObjArgs(pInstance, pName,pMessage, NULL);
    if (pValue != NULL)
        printf("Return of call : %d\n", PyInt_AsLong(pValue));
        result = 0;
        result =-1;
    // Clean up
    return result;


ArchLinux heads-up: I lost 2 days figuring how to compile this because there is no python-dev package. Found out it's as easy as $(pkg-config --cflags --libs python2) as stated in this solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.