element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Code Exchange
  • Technologies
  • More
Code Exchange
Forum Raspberry Pi Python code
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 1 reply
  • Subscribers 47 subscribers
  • Views 351 views
  • Users 0 members are here
  • Code Exchange
  • pyhton
  • c++
Related

Raspberry Pi Python code

Former Member
Former Member over 12 years ago

hi everyone i need some code to control 6 relays via the gpio pins

ill give you the c code i have but i want it in python pls can you help me

 

 

 

#include <stdio.h>

#include <fcntl.h>

#include <sys/mman.h>

 

 

#define IOBASE   0x20000000

 

 

#define GPIO_BASE (IOBASE + 0x200000)

 

 

#define GPFSEL0    *(gpio.addr + 0)

#define GPFSEL1    *(gpio.addr + 1)

#define GPFSEL2    *(gpio.addr + 2)

#define GPFSEL3    *(gpio.addr + 3)

#define GPFSEL4    *(gpio.addr + 4)

#define GPFSEL5    *(gpio.addr + 5)

// Reserved @ word offset 6

#define GPSET0    *(gpio.addr + 7)

#define GPSET1    *(gpio.addr + 8)

// Reserved @ word offset 9

#define GPCLR0    *(gpio.addr + 10)

#define GPCLR1    *(gpio.addr + 11)

// Reserved @ word offset 12

#define GPLEV0    *(gpio.addr + 13)

#define GPLEV1    *(gpio.addr + 14)

 

 

#define BIT_17 (1 << 17)

 

 

#define PAGESIZE 4096

#define BLOCK_SIZE 4096

 

 

struct bcm2835_peripheral {

    unsigned long addr_p;

    int mem_fd;

    void *map;

    volatile unsigned int *addr;

};

 

 

struct bcm2835_peripheral gpio = {GPIO_BASE};

 

 

// Some forward declarations...

int map_peripheral(struct bcm2835_peripheral *p);

void unmap_peripheral(struct bcm2835_peripheral *p);

 

 

int gpio_state = -1;

 

 

////////////////

//  main()

////////////////

int main(int argc, char *argv[]) {

 

 

    if(argc == 2) {

        if(!strcmp(argv[1], "on"))

            gpio_state = 1;

        if(!strcmp(argv[1], "off"))

            gpio_state = 0;

    }

 

 

    if(map_peripheral(&gpio) == -1) {

        printf("Failed to map the physical GPIO registers into the virtual memory space.\n");

        return -1;

    }

 

 

    /* Set GPIO 17 as an output pin */

    GPFSEL1 &= ~(7 << 21); // Mask out bits 23-21 of GPFSEL1 (i.e. force to zero)

    GPFSEL1 |= (1 << 21);  // Set bits 23-21 of GPFSEL1 to binary '001'

 

 

    if(gpio_state == 0)

        GPCLR0 = BIT_17;

    else if(gpio_state == 1)

        GPSET0 = BIT_17;

 

 

    usleep(1);    // Delay to allow any change in state to be reflected in the LEVn, register bit.

 

 

    printf("GPIO 17 is %s\n", (GPLEV0 & BIT_17) ? "high" : "low");

 

 

    unmap_peripheral(&gpio);

 

 

    // Done!

}

 

 

// Exposes the physical address defined in the passed structure using mmap on /dev/mem

int map_peripheral(struct bcm2835_peripheral *p)

{

   // Open /dev/mem

   if ((p->mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {

      printf("Failed to open /dev/mem, try checking permissions.\n");

      return -1;

   }

 

 

   p->map = mmap(

      NULL,

      BLOCK_SIZE,

      PROT_READ|PROT_WRITE,

      MAP_SHARED,

      p->mem_fd,  // File descriptor to physical memory virtual file '/dev/mem'

      p->addr_p      // Address in physical map that we want this memory block to expose

   );

 

 

   if (p->map == MAP_FAILED) {

        perror("mmap");

        return -1;

   }

 

 

   p->addr = (volatile unsigned int *)p->map;

 

 

   return 0;

}

 

 

void unmap_peripheral(struct bcm2835_peripheral *p) {

 

 

    munmap(p->map, BLOCK_SIZE);

    close(p->mem_fd);

}

  • Sign in to reply
  • Cancel
Parents
  • callum311
    0 callum311 over 11 years ago

    you should be able to use that as long as you have the arduino repository if you are on raspberry pi you will need an arduino board and install the software

     

    sudo apt-get install arduino

     

    or even have a look on you tube but just remember on the standard gpio pins it is all output not input

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • callum311
    0 callum311 over 11 years ago

    you should be able to use that as long as you have the arduino repository if you are on raspberry pi you will need an arduino board and install the software

     

    sudo apt-get install arduino

     

    or even have a look on you tube but just remember on the standard gpio pins it is all output not input

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube