Raspberry Pi Pico (RP2040) I2C Example with MicroPython and C/C++ | Digi-Key Electronics

Sdílet
Vložit
  • čas přidán 23. 07. 2024
  • Inter-Integrated Circuit (I2C) is a common communication protocol used by many microcontrollers and sensors to pass data back and forth. It relies on 2 pins for communication:
    - Clock (SCL)
    - Data (SDA)
    The pins to control these lines are required to be open drain in order to support more than 2 devices on the bus. As a result, external pull-up resistors are required on the lines. The sensor breakout board we use in the video already contains these resistors, so you do not need to provide your own.
    In this video, we show you how to use I2C from the Raspberry Pi Pico to communicate with an ADXL343 accelerometer. We give an example in MicroPython as well as C/C++.
    A written form of this tutorial along with the code can be found here: www.digikey.com/en/maker/proj...
    More information about I2C can be found in this article: www.analog.com/en/technical-a...
    We recommend following this guide to set up the toolchain and VS Code if you are working with the Raspberry Pi Pico C/C++ SDK: www.digikey.com/en/maker/proj...
    I2C is often used for relatively low data rates (100 or 400 kHz), although faster modes (1 MHz and 3.4 MHz) exist for devices that support them. Unlike SPI, I2C uses just 2 lines for every device on the same bus, which keeps the required pin count low. As a result, device packages can be smaller, which is why I2C is popular with sensor manufacturers.
    Product Links:
    www.digikey.com/en/products/d...
    www.digikey.com/en/products/d...
    Related Videos:
    Intro to Raspberry Pi Pico and RP2040 - MicroPython Part 1: Blink - • Intro to Raspberry Pi ...
    Intro to Raspberry Pi Pico and RP2040 - C/C++ Part 1: VS Code and Blink - • Intro to Raspberry Pi ...
    Intro to Raspberry Pi Pico and RP2040 - C/C++ Part 2: Debug with Picoprobe - • Intro to Raspberry Pi ...
    Related Project Links:
    www.digikey.com/en/maker/proj...
    www.digikey.com/en/maker/proj...
    www.digikey.com/en/maker/proj...
    Learn more:
    Maker.io - www.digikey.com/en/maker
    Digi-Key’s Blog - TheCircuit www.digikey.com/en/blog
    Connect with Digi-Key on Facebook / digikey.electronics
    And follow us on Twitter / digikey
  • Věda a technologie

Komentáře • 14

  • @grantpeterson2524
    @grantpeterson2524 Před 3 lety +1

    I just recently got my Pico from you guys about 2 months ago before this video was published. Love the clear but concise language and voice!

  • @KellyDiversified
    @KellyDiversified Před 9 měsíci

    Great speaker and explanation. Moves a little fast, but can always rewind and repeat - thanks!

  • @cattflap1447
    @cattflap1447 Před 3 lety +1

    Excellent, thank you 😁

  • @MUHAMMADYAWARIFRAHEEM
    @MUHAMMADYAWARIFRAHEEM Před 3 lety +2

    Very informative video

  • @peterbell4736
    @peterbell4736 Před 3 měsíci

    I find many tutorials for using pico as an i2c master (controller), but not a lot of help using pico as an i2c slave (responder).
    I'm aware of the library created by Don Perron (and the epmoyer derivative) but haven't had a great deal of success with it.
    I have control system using a network of RPis, and currently using chips such as the mcp23017 for i/o. I wish to replace the various i/o devices with picos.

  • @TheStrafendestroy
    @TheStrafendestroy Před rokem

    So I have looked at my i2c line with a logic analyzer and keep gettin g a setup to read followed by a NAK even though im trying to write to the device. I 100 percent have to right address for the device 102 for the EZO_RTD. I did not pull up my resistor in internally since i have them externally. Help would be appreciated I have been trying ti fix this for days
    void i2c_setup(int SDA, int SCL,uint clock_speed,i2c_inst_t* i2c_port)
    {
    i2c_init(i2c_port, clock_speed); // This example will use I2C0 on the default SDA(Pin 0) and SCL(Pin 1)
    gpio_set_function(SDA, GPIO_FUNC_I2C); // Serial Clock
    gpio_set_function(SCL, GPIO_FUNC_I2C); // Serial Data line
    gpio_pull_up(SDA);
    gpio_pull_up(SCL);
    }
    int main()
    {
    int SDA = 0;
    int SCL = 1;
    // Vairable for read and write blocks
    uint8_t rxbuff[40]; // Receiving Buffer
    const uint8_t command[2]= "r"; // Command
    int len_bytes_command = strlen(command);
    int max_length_receive_bytes = 40;
    uint8_t address = 102;


    // Enable USB UART so we can print status output
    stdio_init_all();
    i2c_setup(SDA,SCL,400*1000,i2c0);


    while(1)
    {
    int written = i2c_write_blocking(i2c0,address,command,1,false);
    sleep_ms(600);
    printf("Bytes:%d,Address: %d
    ",written,address);

    }
    return 0;
    }

  • @scienceofart9121
    @scienceofart9121 Před 2 lety

    I have a question, where are the DEVID constant comes from ? My peripheral adress is 0x6c and it returns x01

  • @ariaas1096
    @ariaas1096 Před 2 lety

    I want to run and get data from GY-271 with raspberry pi pico and I don't know how I must do it. It works with I2C. could you help, please?

  • @cstanford8046
    @cstanford8046 Před 4 měsíci

    Is the SCL really 400kHz? I measured 250kHz after looking at it on a logic analyzer.

  • @eat-myshorts
    @eat-myshorts Před rokem

    I^2 C multiple devices using Raspberry Pi Pico

  • @gorge9774
    @gorge9774 Před 2 lety +1

    Hi, i get error that i could not communicate with ADXL343,
    I don't have an accelerometer as you so i changed ADXL343_ADDR to:
    ADXL343_ADDR = 0x4c, So it should work fine.
    I would be grateful if you could help me solve this problem
    By the way my accelerometer is Grove - MMA7660FC
    bytearray((DEVID,)) = b'\xe5'
    data = '\x00'