user61715: Background: I change the job and in the new company you need to pay pizza for the whole team if you forget to lock your screen :-)
I want it to be automated: I walk away and the screen should lock. No additional action should be required. Keyboard shortcuts, special mouse movements or unplugging devices from USB are not valid solutions.
How could this be solved (with or without arduino)?
Please write slowly, I never worked with sensors or arduino before :-)
I need this for Mac and Ubuntu.

user61763: The simplest solution would be using a pressure sensor (pressure pad) in your seat. Or a pressure mat if you use a standing up desk.
Ideally, you can calibrate it with an Arduino and send a "Windows Key + L" or a "Ctrl + Cmd + Q" if it is an Apple. 
I reckon no other solution can be simpler to implement nor to maintain. 
If you want a slightly more refined solution you can connect a scale, tare it with your weight and set a weight threshold for the Arduino to send the signal.
Edit: In ubuntu, you can set up any key combination to lock the screen. But the rest of the answer stands. 

user61320: Use an obscure window manager which other people don't know how to use.
I use i3 and like to deliberately leave my workstation unlocked, because it's funny when I come back and find people pressing various key combinations to try and activate the group chat window (sending a funny message in my name is how they would prove they've 'hacked me') which I put into a hidden buffer.
Two and a half years in this role and they still haven't figured it out...all they need to do is RTFM...

user47947: Place a ultrasonic distance sensor HC-05 close to your monitor, facing towards your chest. Check the distance measured against a defined threshold (potentiometer?) and send a key press to your PC to lock it if it is exceeded. 
I can go into more details if wanted, it's actually a project I thought about implementing before. 
Edit: Whoops, @Eduardo Trápani suggested it already, but I think I can add to that. 
I would use a digispark-like chip that can emulate key presses (WIN + L), this won't get you into trouble for installing software on your PC. 

user59786: A single distance sensor, properly placed, should be enough to detect when you leave. That's the arduino part.
With python you can then use the serial over USB to read the output from the arduino. And run the command to activate the screen lock, according to your reading.

user61741: At a previous job we also had the rule that the screen had to be locked (usually done   by pressing Win+L).
However, there was no pizza; instead, the computer was used to send mass e-mails to colleagues. The topics of the e-mails was as varied as the imaginations of people:

invitations to various events;
invitations to various celebrations at the desk of the not locked computer;
selling of expensive items for very low prices, providing whatever reasons;
etc etc

Many times, the content of the e-mail was the result of team-work.
The only limit was that the messages should use a decent language and not be (very) offensive. Also, I have never heard of e-mails sent in order to really harm the forgetful person (e.g. termination notice, offensive language to bosses or cutomers...).

The easiest and cheapest method is to just use Win+L. After you buy pizza for everybody for a few times, and have to endure their jokes / sarcasm at the same time, you will never forget to lock it again.
It is like learning to not pee in the bed during the night. It happens a few times, until you get it right.

For a technical solution, the sky is the limit.
As an (impractical) joke, you may want to have a look at the OMGWTF and the winners.
Example: configure the screensaver to kick in and lock the screen after ~ 5 seconds of inactivity (that is roughly the time you need to stand up and walk away, before any colleague has a chance to start typing without you noticing). It will create some trouble for you also, but you are sure that the computer will never remain unlocked. No additional software, hardware or work required - besides moving the mouse and hitting buttons on the keyboard. At the same time, you will become the most productive employee in the company. :)

On a more serious note, the purpose of that rule is to make people more aware of the security needs, and the need of them being involved in the process. Usually, locking the screen is the least one can do towards security, even before choosing good passwords - screen not locked, password not needed at all.
If one fails to do even that (lock the computer screen when the computer remains unattended), then there is a big chance that the other security rules are not followed either.

user31038: If you chair has a gas cylinder to adjust height, it sinks a bit when sat in. My dip is about a centimeter. That's more than enough for a beam-interruption or reflection sensor, a hall effect sensor, or even a dead-simple magnetic reed switch, like those used by home alarms to monitor windows. 
You could have the reed switch turn on/wake up a micro like an ESP8266, which uses ESP-NOW to talk to another ESP plugged into a usb serial on the desktop. Once the message is sent, the micro can go into deep sleep. The desktop's serial port is read by the lang of your choice; node.js, python, c#, etc, and runs a command or fires a key combo to lock the workstation.

user4143: RFID (NFC) might be the way to go.  Have an RFID tag in a wristband and an RFID reader by the side of (or under) your keyboard. As long as you keep your hands close to the keyboard the RFID reader should detect your wristband. When it doesn't sense it for a pre-defined period of time send a signal to the PC to trigger it to lock. It could then trigger the unlock when your wristband is back in range again.

user61750: Bluetooth!
In the same vein as Gerben's comment, it is quite easy to use your phone for this, assuming that both your PC and your phone have bluetooth (and you take your phone with you, when you leave your seat). Moreover, if your PC does not have BT, cheap BT dongle will solve this.
Next step is writing a script that gets a list of all nearby/found BT devices and if your phone is not on that list, it just sends system call to lock.
As I have actually tried this once (although with a WIN PC), I can say it works more reliably than I would have initially expected (YMMV, obviously).
The great thing is that you (potentially) do not need any extra hardware, just a bit of code.

user61730: Sorry, no Arduino required, but you probably already have a web cam on your desktop. If you don't mind being observed like Schrodinger's Cat, you can use a small utility like YawCam in Windows or Motion on Linux to trigger the screen lock when you are no longer visible at your console.

61715: <p>Background: I change the job and in the new company you need to pay pizza for the whole team if you forget to lock your screen :-)</p>

<p>I want it to be automated: I walk away and the screen should lock. No additional action should be required. Keyboard shortcuts, special mouse movements or unplugging devices from USB are not valid solutions.</p>

<p>How could this be solved (with or without arduino)?</p>

<p>Please write slowly, I never worked with sensors or arduino before :-)</p>

<p>I need this for Mac and Ubuntu.</p>
 61763: <p>The simplest solution would be using a pressure sensor (pressure pad) in your seat. Or a pressure mat if you use a standing up desk.</p>

<p>Ideally, you can calibrate it with an Arduino and send a "Windows Key + L" or a "Ctrl + Cmd + Q" if it is an Apple. </p>

<p>I reckon no other solution can be simpler to implement nor to maintain. </p>

<p>If you want a slightly more refined solution you can connect a scale, tare it with your weight and set a weight threshold for the Arduino to send the signal.</p>

<p>Edit: In ubuntu, you can set up any key combination to lock the screen. But the rest of the answer stands. </p>
 61320: <p>Use an obscure window manager which other people don't know how to use.</p>

<p>I use <a href="https://i3wm.org/" rel="nofollow noreferrer">i3</a> and like to deliberately leave my workstation unlocked, because it's funny when I come back and find people pressing various key combinations to try and activate the group chat window (sending a funny message in my name is how they would prove they've 'hacked me') which I put into a hidden buffer.</p>

<p>Two and a half years in this role and they still haven't figured it out...all they need to do is RTFM...</p>
 47947: <p>Place a ultrasonic distance sensor HC-05 close to your monitor, facing towards your chest. Check the distance measured against a defined threshold (potentiometer?) and send a key press to your PC to lock it if it is exceeded. </p>

<p>I can go into more details if wanted, it's actually a project I thought about implementing before. </p>

<p>Edit: Whoops, @Eduardo Trápani suggested it already, but I think I can add to that. </p>

<p>I would use a digispark-like chip that can emulate key presses (WIN + L), this won't get you into trouble for installing software on your PC. </p>
 59786: <p>A single <a href="https://create.arduino.cc/projecthub/MisterBotBreak/how-to-use-an-ultrasonic-sensor-181cee" rel="nofollow noreferrer">distance sensor</a>, properly placed, should be enough to detect when you leave. That's the arduino part.</p>

<p>With python you can then use the serial over USB <a href="https://riptutorial.com/arduino/example/22868/first-serial-communication-between-arduino-and-python" rel="nofollow noreferrer">to read the output from the arduino</a>. And run the command to activate the screen lock, according to your reading.</p>
 61741: <p>At a previous job we also had the rule that the screen had to be locked (usually done   by pressing Win+L).</p>

<p>However, there was no pizza; instead, the computer was used to send mass e-mails to colleagues. The topics of the e-mails was as varied as the imaginations of people:</p>

<ul>
<li>invitations to various events;</li>
<li>invitations to various celebrations at the desk of the not locked computer;</li>
<li>selling of expensive items for very low prices, providing whatever reasons;</li>
<li>etc etc</li>
</ul>

<p>Many times, the content of the e-mail was the result of team-work.</p>

<p>The only limit was that the messages should use a decent language and not be (very) offensive. Also, I have never heard of e-mails sent in order to really harm the forgetful person (e.g. termination notice, offensive language to bosses or cutomers...).</p>

<hr>

<p>The easiest and cheapest method is to just use Win+L. After you buy pizza for everybody for a few times, and have to endure their jokes / sarcasm at the same time, you will never forget to lock it again.</p>

<p>It is like learning to not pee in the bed during the night. It happens a few times, until you get it right.</p>

<hr>

<p>For a technical solution, the sky is the limit.</p>

<p>As an (impractical) joke, you may want to have a look at the <a href="https://en.wikipedia.org/wiki/The_Daily_WTF#Olympiad_of_Misguided_Geeks" rel="noreferrer">OMGWTF</a> and the <a href="https://thedailywtf.com/articles/Announcing-the-OMGTWTF-Winner" rel="noreferrer">winners</a>.</p>

<p>Example: configure the screensaver to kick in and lock the screen after ~ 5 seconds of inactivity (that is roughly the time you need to stand up and walk away, before any colleague has a chance to start typing without you noticing). It will create some trouble for you also, but you are sure that the computer will never remain unlocked. No additional software, hardware or work required - besides moving the mouse and hitting buttons on the keyboard. At the same time, you will become the most productive employee in the company. :)</p>

<hr>

<p><strong>On a more serious note</strong>, the purpose of that rule is to make people more aware of the security needs, and the need of them being involved in the process. Usually, locking the screen is the least one can do towards security, even before choosing good passwords - screen not locked, password not needed at all.</p>

<p>If one fails to do even that (lock the computer screen when the computer remains unattended), then there is a big chance that the other security rules are not followed either.</p>
 31038: <p>If you chair has a gas cylinder to adjust height, it sinks a bit when sat in. My dip is about a centimeter. That's more than enough for a beam-interruption or reflection sensor, a hall effect sensor, or even a dead-simple magnetic reed switch, like those used by home alarms to monitor windows. </p>

<p>You could have the reed switch turn on/wake up a micro like an ESP8266, which uses ESP-NOW to talk to another ESP plugged into a usb serial on the desktop. Once the message is sent, the micro can go into deep sleep. The desktop's serial port is read by the lang of your choice; node.js, python, c#, etc, and runs a command or fires a key combo to lock the workstation.</p>
 4143: <p>RFID (NFC) might be the way to go.  Have an RFID tag in a wristband and an RFID reader by the side of (or under) your keyboard. As long as you keep your hands close to the keyboard the RFID reader should detect your wristband. When it doesn't sense it for a pre-defined period of time send a signal to the PC to trigger it to lock. It could then trigger the unlock when your wristband is back in range again.</p>
 61750: <h3>Bluetooth!</h3>

<p>In the same vein as Gerben's comment, it is quite easy to use your phone for this, assuming that both your PC and your phone have bluetooth (and you take your phone with you, when you leave your seat). Moreover, if your PC does not have BT, cheap BT dongle will solve this.</p>

<p>Next step is writing a script that gets a list of all nearby/found BT devices and if your phone is not on that list, it just sends system call to lock.</p>

<p>As I have actually tried this once (although with a WIN PC), I can say it works more reliably than I would have initially expected (YMMV, obviously).</p>

<p>The great thing is that you (potentially) do not need any extra hardware, just a bit of code.</p>
 61730: <p>Sorry, no Arduino required, but you probably already have a web cam on your desktop. If you don't mind being observed like Schrodinger's Cat, you can use a small utility like <a href="https://www.yawcam.com/" rel="noreferrer">YawCam</a> in <a href="https://www.maketecheasier.com/use-the-webcam-as-surveillance-camera" rel="noreferrer">Windows</a> or <a href="https://motion-project.github.io/" rel="noreferrer">Motion</a> on <a href="https://www.maketecheasier.com/setup-motion-detection-webcam-ubuntu/" rel="noreferrer">Linux</a> to trigger the screen lock when you are no longer visible at your console.</p>
