Exploring Prolog and GPIO Control on the Raspberry Pi 400

Kenichi Sasagawa
2 min readSep 19, 2024

--

Operation Check

I tested whether N-Prolog ver 3.20 works on the Raspberry Pi 400. Since I increased the cell area, I was concerned whether the Raspberry Pi’s memory would be sufficient. After confirming, I found that it works fine. It operates normally. I believe it will function similarly on other recent Raspberry Pi models with larger memory, not just the Raspberry Pi 400.

GPIO

The Raspberry Pi 400 exposes GPIO (General Purpose Input/Output) for users to control input and output. Using this, you can control various electronic components such as turning LEDs on and off, emitting ultrasonic waves, and receiving signals. When you compile N-Prolog on the Raspberry Pi 400, predicates for automatically controlling GPIO are built in. It utilizes WiringPi. The features of WiringPi that are called from these predicates are as follows:

N-Prolog <==================================> C
wiringpi_spi_setup(ch speed) <===> wiringPiSPISetup (SPI_CH, SPI_SPEED)
wiringpi_setup_gpio <===> wiringPiSetupGpio()
pin_mode(n, 'output) <====> pinMode(n, OUTPUT) or 'input -> INPUT 'pwm-output -> PWM_OUTPUT
digital_write(n, v) <===> digitalWrite(n, v)
digital_write_byte(v) <===> digitalWriteByte(value)
digital_read(pin) <===> digitalRead(pin)
delay(howlong) <===> void delay(unsigned int howLong)
delay_microseconds(howlong) <===> void delay_microseconds(unsigned int howLong)
pull_up_dn_control(pin, pud) <===> pullUpDnControl(pin,pud)
pwm_set_mode('pwm_mode_ms) <===> pwmSetMode(PWM_MODE_MS); or 'pwm_mode_bal -> PWM_MODE_BAL
pwm_set_clock(n) <===> pwmSetClock(n)
pwm_set_range(n) <===> pwmSetRange(n)
pwm_write(pin, value) <===> pwmWrite(pin , value)

timer_microseconds/1
usage: timer_microseconds(on). timer on
timer_microseconds(off). timer off
timer_microsecons(X). unify X elapsed time. float number ,unit seconds , valid digits microsecnods
see tests/measure.pl

Robot Control

By using the GPIO functionality, you can control toy robots and other hardware for fun. Learning both programming and hardware control is an enjoyable experience. For example, controlling a robot mouse that navigates and escapes a maze is straightforward when written in Prolog. Algorithms for escape, such as the left-hand method and memory of past branching points, can be described concisely. This would be a valuable experience for students, and I hope it will help them understand the usefulness of predicate logic and logic programming.

N-Prolog is open source and released under the BSD2 license on GitHub. Feel free to use it. sasagawa888/nprolog: interpreter and compiler to be compatible with ARITY/PROLOG(MS-DOS) (github.com)

--

--