0

I've come across a few tutorials for adding an rtc module to a pi. They all include a step in which you do echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device. When I try this, I get a bash: ... Permission denied. If I pipe into a tee command to write to /sys/class..., I get tee: ... invalid argument. If I try bash -c or sh -c before putting the echo command in quotes, I get an Invalid argument error or sh: echo: I/O error. I always run sudo modprobe commands beforehand, to i2c-bcm2708 and rtc-ds1307.

I'll note that my rtc device is listed as ds3231, not ds1307, but modprobe on ds1307 is the one that works, so I am confused on that point as well.

What's my next step.

Some pi details: I am using a model b with 512 mb of ram. The OS is Raspbian.

  • try adding sudo to the front sudo echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device. If that does not solve the problem we will need a little more information: which model Pi are you using? What OS and version are you using? Note: please add these details to your question not the comments. – Steve Robillard Mar 10 '16 at 0:59
  • sudo echo also does not work – cfye14 Mar 10 '16 at 1:22
  • instead of echoing the text to the file open it with an editor (create it if necessary) and add the text. Also, see my previous comment re: additional info. – Steve Robillard Mar 10 '16 at 1:23
  • well, first of all, it was root-only write permissions on that file, and nothing else, so I had to change that. When I was able to open the file and added the appropriate text, I got an invalid argument error. and that's with nano. If I try to open the file with a graphical text editor, I get an i/o error. – cfye14 Mar 10 '16 at 1:29
  • what is the ls -la output for the directory where the file is, and what exactly did you enter in the file? – Steve Robillard Mar 10 '16 at 1:32
3

The problem with your original

echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device

command is that only root can write to the /sys/class.. file. Normally prefixing a command with sudo will give you root privileges, but because of the way that the command is interpreted, the echo ds1307 0x68 will be run as root, but not the > part which writes to the /sys/class... file. To run the whole command as root, you can do:

sudo bash
echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device

This will put you into a root shell, and run the whole command as root. Note that you will be running as user root until you exit from the shell.

You can alternately do:

echo 'ds1307 0x68' | sudo tee /sys/class/i2c-adapter/i2c-1/new_device

The reason you are not seeing your permission changes stick (in the comments), is that the file is on a sysfs virtual file system, which is re-created each time you boot.

See here for a similar question: https://stackoverflow.com/questions/84882/sudo-echo-something-etc-privilegedfile-doesnt-work-is-there-an-alterna

See here for more info on sysfs: https://en.wikipedia.org/wiki/Sysfs

0

I ran into this problem, and it seems to stem from the device already being in use/busy. If you run i2cdetect -y 1 and see the device showing UU in the output, it is already in use. When in use, you will get an I/O error trying to add it as a new device. Try rebooting and then running the command again. If it has been correctly added, a device at /dev/rtc0 should also show up.

When actually running the command, I have had the most success from a root terminal. Enter sudo bash, then type the echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device without any sudo or other modifiers. For this bash -c should work as well.

The use of ds1307 is correct for the ds3231, they are designed to be compatible in terms of communication protocol.

  • Even without active scripts, if I reboot with the rtc module connected, i2cdetect will show a UU in spot 68. However, if I connect the module once the os is running, I do not see a UU. However, I get all the same errors on all the commands. – cfye14 May 2 '16 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.