Thursday, May 26, 2016

Raspberry "Hello World" in C and Python

Connection to pins GND yellow wire 5th pin from left to right
and GPIO17 green wire 6th from left to right
 Circuit with led and a resistor (~330 Ohms) 



To use Python nothing have to be installed 
#!/usr/bin/python
from gpiozero import LED
from time import sleep
led = LED(17)
for num in range(1,10):
    led.on()
    sleep(1)
    led.off()
    sleep(1)
To use the C language, install the wiringPi library

# git clone git://git.drogon.net/wiringPi
# cd wiringPi
#./build

Pin 0 is the GPIO17
blink.c
#include <wiringPi.h>

#define LED 0

int main (void)
{
  int i ;
  wiringPiSetup() ;
  pinMode (LED, OUTPUT) ;
  for (i=0 ; i < 10; i++)
  {
    digitalWrite(LED, HIGH) ;
    delay(500) ;
    digitalWrite(LED, LOW) ;
    delay(500) ;
  }
  return 0 ;
}
# gcc -L/usr/local/lib -lwiringPi -lwiringPiDev -lpthread -lm -I/usr/local/include -Winline -pipe -o blink blink.c
# export WIRINGPI_GPIOMEM=1
# ./blink

WIRINGPI_GPIOMEM variable is to use the /dev/gpiomem instead of /dev/mem. Without this variable set you need root privilegies to execute the program
http://wiringpi.com
https://www.raspberrypi.org/learning/python-quick-reaction-game/worksheet


Pins for reference, in this example we use the pin G017 and Ground(GND)

No comments:

Blog Archive