Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Hardware > Linux - Embedded & Single-board computer
User Name
Linux - Embedded & Single-board computer This forum is for the discussion of Linux on both embedded devices and single-board computers (such as the Raspberry Pi, BeagleBoard and PandaBoard). Discussions involving Arduino, plug computers and other micro-controller like devices are also welcome.


  Search this Thread
Old 01-27-2021, 06:45 PM   #1
LQ Newbie
Registered: Jan 2021
Posts: 4

Rep: Reputation: Disabled
Unhappy I2C commands not being sent properly

Hello everyone, it's my first time posting so please be nice.

I'm working on a project where I have a small SBC (not a raspi nor a beaglebone, but the problem is my software so let's move on to that) and the program I've written is having trouble writing to the i2c interface.

I can open the i2c file descriptor, and I can read from it just fine. BUT when I write to it, it returns the correct number of written bytes, correctly sends the first byte, but the rest are all zeros. This problem stumped me until I connected my board to an oscilloscope.

It's sending the correct i2c address, and the correct register location, but the data is all zeros.
I can't seem to post images so what I'm seeing on my scope is: 0xD0 0x7E 0x00
What I should be seeing is 0xD0 0x7E 0xB6.

Below is my code. If someone can tell me where I've gone wrong I would greatly appreciate it.
*The calls to LE_INFO are just debug statements that show up in logs*

#include <linux/i2c-dev.h>

int main(){

	int i2c = open("/dev/i2c-4", O_RDWR);

	if(i2c < 0){
		LE_INFO("The i2c bus could not be opened, error: %d", i2c);
		LE_INFO("The i2c bus was opened.");

	//you have the bus, now send the address of the device you want to communicate with.
	if((ioctl(i2c, I2C_SLAVE, 0x68)) < 0){
		LE_INFO("Could not acquire control of i2c bus");

	int location[] = {0,0};
	int res = 0;

	location[0] = 0x7E; //command register
	location[1] = 0xB6; //command

	LE_INFO("register location %d, command %d", location[0], location[1]);
	//setup bus to read a byte. 
	if((res = write(i2c, location, 2)) != 2){ //write the 
		LE_INFO("read register location was not written to i2c bus, %d bytes written",res);



Old 01-28-2021, 12:37 PM   #2
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,813
Blog Entries: 13

Rep: Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875Reputation: 4875

Your location array should be characters, "char", not int.

What you're doing is consistent with the I2c_dev library, here's a similar example showing SMBus as well as read/write operations:, but nice work, what you have for code flow is fine, I think it's just the size of the array elements which is your problem, change it to bytes:
char location[2] = {0,0};
1 members found this post helpful.
Old 01-28-2021, 12:50 PM   #3
LQ Newbie
Registered: Jan 2021
Posts: 4

Original Poster
Rep: Reputation: Disabled
AHHHH it finally worked! Thank you!!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
All mail sent to postfix are being sent to root@localhost instead of virtual users quadcricket Linux - Server 8 08-09-2016 06:16 PM
i2c-dev.h missing i2c read/write functions dbrazeau Programming 1 12-17-2015 10:40 PM
How to forward IOCTL's from i2c-dev.c to adapter driver (i2c-rt3352.c) Linux - Kernel 2 08-23-2011 02:03 AM
[SOLVED] No /dev/i2c-0 entry after insmod i2c-dev sajnanazeer Linux - Newbie 1 08-10-2011 07:39 AM
Help! Problems with i2c-dev. No more /dev/i2c-* em1l Fedora 0 10-28-2005 05:48 AM > Forums > Linux Forums > Linux - Hardware > Linux - Embedded & Single-board computer

All times are GMT -5. The time now is 03:53 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration