I'm trying to manually edit the w1_slave file of a DS18B20 Sensor. Using nano to edit the file, when I manually change the temperature and go to save and exit, I get 'permission denied'.

Is there anyway of doing this?

Picture shows console after attempting to write file 'w1_slave'

  • 2
    Why do you want to write the temperature to the sensor? What do you want to achieve?
    – Dirk
    Dec 16, 2020 at 2:51
  • Hi, i'm trying to change the temperature manually and stream that manual reading to AWS so an event is triggered. It is a project i am undertaking. Dec 16, 2020 at 9:40
  • Check the file privileges, is it writable ? Use the command ls with appropriate options. Or create a dummy file and then modify your code to read that file instead. Dec 16, 2020 at 12:25
  • I have given the file full access using 'chmod + x w1_slave' and still no luck. I'm beginning to think this file cannot be edited regardless. Dummy file was last resort as i need the original file to prove a point in the project. Dec 16, 2020 at 12:38
  • I’m trying to figure out why you want to do this? If you send a reading to the w1_slave it could break the device. Wouldn’t a test script that included the desired temp be more suitable?
    – 00BEAR
    Dec 16, 2020 at 12:47

4 Answers 4


Setting up the sensor: We need to load the drivers for the 1-wire comms and the temp sensor into the Pi kernel. Modprobe is a Linux program to add a loadable kernel into the Linux kernel. In your terminal enter:


sudo modprobe w1-gpio


sudo modprobe w1-therm

3.)Now change your working directory (cd) to:

cd /sys/bus/w1/devices/

4.)This is where the devices running on the 1-wire will be. So to find our newly created device just list the contents of the directory with ls.


Now you should see something listed like

28-00000222fd25 w1_bus_master1

5.)This is the serial number for the device. To interrogate that device we need to go into its directory. Make sure you use the serial number of your own sensor!!

 cd 28-00000222fd25

6.)The sensor writes to a file called w1_slave so if we just read that file we can now finally know what temperature it is. Enter:

cat w1_slave
  • Hi there, thanks for the answer. I have managed to completed these steps already although, i'm wondering if a manual temperature can be set within the w1_slave? I'm trying to hack the device to produce a false temperature. cheers. Dec 16, 2020 at 9:38

Please read the driver documentation: https://owfs.org/index_php_page_ds18b20.html

There you will find that the temp value is read-only:


read-only, floating point
Measured temperature with 12 bit resolution.

but the alarm values is read/write


read-write, integer
Shows or sets the lower limit for the high temperature alarm state.


read-write, integer
Shows or sets the upper limit for the low temperature alarm state. 

And if you read the datasheet

Quote: Byte 0 and byte 1 of the scratchpad contain the LSB and the MSB of the temperature register, respectively. These bytes are read-only.


Lets Set up getTemp.py

7.)Go back to the root directory and make a new directory (mkdir) called tempLog.


8.)Make a new dir file

mkdir tempLog

9.)Go into the new directory and create a new python file in nano.

cd tempLog

10.)Edit the file

sudo nano getTempSQL.py

11.)Copy the code below taking care to use your own value for the sensor as well as the database.

import os
import time
import datetime
import glob
import MySQLdb
from time import strftime
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
temp_sensor = '/sys/bus/w1/devices/28-01161d4fdbee/w1_slave'
# Variables for MySQL
db = MySQLdb.connect(host="localhost", user="root",passwd="Password", db="temp_database")
cur = db.cursor()
def tempRead():
    t = open(temp_sensor, 'r')
    lines = t.readlines()
    temp_output = lines[1].find('t=')
    if temp_output != -1:
        temp_string = lines[1].strip()[temp_output+2:]
        temp_c = float(temp_string)/1000.0
    return round(temp_c,1)
while True:
    temp = tempRead()
    print temp
    datetimeWrite = (time.strftime("%Y-%m-%d ") + time.strftime("%H:%M:%S"))
    print datetimeWrite
    sql = ("""INSERT INTO tempLog (datetime,temperature) VALUES (%s,%s)""",(datetimeWrite,temp))
        print "Writing to database..."
        # Execute the SQL command
        # Commit your changes in the database
        print "Write Complete"
        # Rollback in case there is any error
        print "Failed writing to database"

Allow python script work autonomously, putting a temperature reading into the database every 5 minutes. To do this we will use Crontab, which is a handy unix tool to schedule jobs. A good explanation of what crontab is can be found here.

12.)To open Crontab enter the following in your terminal.

sudo crontab -e

13.)Inside the Crontab file enter the following at the bottom. This simply runs our Python script every 5 minutes. If you have used different file and folder names, adjust them accordingly.

/usr/bin/python2 /home/pi/readTempSQL.py > /home/pi/cronjoblog 2>&1

8.)Now lets check that the Python script actually entered data into the MySQL database. Log back into MySQL and USE the temp_database. We can then query the tempLog table:

mysql -u root -p

14.)Use the database with the following.

USE temp_database;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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